Chapter 2. Expressions

This chapter will introduce some basic expressions. Topic covered will be operators, precedence, and comments.

2.1 - What are Operators?

To someone who has little to no prior with programming, the concept of operators might be odd, although most people have used them before without knowing so. An operator is a symbol that denotes a function or operation. Operators can be either unary (operates on a single expression), binary (operates on two expressions), or ternary (operates on three expressions).

Surprisingly, most people have used operators since they were in primary school. The most common operators used are +, -, *, and /. These are all examples of a binary operator, since they have a left side and a right side. Given the expression 5 + 8, 5 is the left operand, + is the addition operator, and 8 is the right operand. This exact notation is a valid Hassium expression.

We can display the results of some mathematical operations using the println function. Run the following program on your terminal.

func main () {
    println (2 + 2);
    println (8 * 4);
    println (8 * 4 + 2);
    println (8 * (4 + 2));
}

The program displays the following to the console:

4
32
34
48

The first two expressions are straightforward. 2 + 2 returns 4, which displays to the terminal and 8 * 4 returns 32 to the terminal. The third statement in the program is 8 * 4 + 2, which returns 34. This is because of the order of operations (sequence in which operators are given precedence). Multiplication has a higher operator precedence (order of operations) than addition, so the multiplication operation is executed first, returning 32. Following the multiplication operation is the addition operation, which takes the result of the multiplication and adds it with 2, returning a final result of 34. The fourth and final statement in the program has the exact same numbers and operations as the prior but instead returns 48. This is because of the use of parentheses to give precedence to the addition operation. The first operation to execute here is 4 + 2, which returns 6, which is then given as the right operand for the multiplication by 8, returning 48.

Operators can operate differently based on which data type (type of constant or variable) they are given. When two numbers are given to the + operator, Hassium adds them together and returns the result. When two strings are given to the + operator, Hassium concatenates (joins) the strings together and returns.

Run the following program.

func main () {
    println (4 + 5);     # this line prints 9
    println ("4" + "5"); # this line prints 45
}

The first line sees the two integers and computes their sum, the second line sees two strings and joins them together.

2.2 - Comments

In the last code example in 2.1, after the semicolons on each line there was an octothorp followed by a sentence. This is known as a comment (note left by a developer in code). The purposes of comments are to explain what your code is doing either for yourself to look back on or for other developers to understand your writing. Think of it like putting a sticky-note inside a book.

2.2.1 Comments - Single Line

The octothorp denotes a single-line comment (a comment that ends once the line it is declared on ends). This can go after a statement like the code example in 2.1, or it can be on a line by itself. Anywhere, as long as it does not interfere with the actual code. Take the following example.

# the main entry point of the program
func main () {
    println ("Hello, World!"); # this prints "Hello, World!"
    # println ("You can't see me!");
    println ("My time is now.");
}
# end of program

When run, this program outputs the following:

Hello, World!
My time is now.

Surprised? Take another look at the second line of main. Even though there is a perfectly valid statement, that line did not run as it was prefixed with an octothorp, and treated as a comment. This can be very useful for debugging programs, where you might not want to delete code from your file, but instead temporarily disable it.

2.2.2 Comments - Multi Line

If you have a comment to leave that is more than three or four lines, it can become troublesome to prefix each line with an octothorp. Hassium offers the luxary of multi-line (comments that can stretch infinitely until a terminator is reached) comments for just this purpose. This means that instead of ending with a newline, a multi-line comment will continue until it is terminated.

Here is an example of such a thing.

func main () {
    $ Hello, I am a multi-line
    comment, I will continue to be
    so until the second doller sign
    is encountered.
    $
}

In Hassium, a multi-line comment starts and ends with a dollar sign. In this example, the comment stretched over 5 lines, none of which were scanned in by the Hassium compiler, causing the program to do nothing.

Chapter 2 - Exercises

Exercise 2.1

Write three different programs that all display the number 1.25.

Exercise 2.2

Using only the numbers 3, 6, and 9. Write a program that displays the number 63.

Exercise 2.3

Add single line comments to each of the lines in the programs above, explaining what each line does. Add a multi-line comment to the top of each source file explaining what the program does.

results matching ""

    No results matching ""