-
Notifications
You must be signed in to change notification settings - Fork 3
Program Flow
Program flow refers to the order in which statements within a function are executed, this order can be changed by using conditional statements or looping.
The most basic way to control program flow is an if
statement. It checks to see if a condition in the form of an expression is true
, and if so, executes the statements contained in the if
body. You can also add an else
to an if
statement, which executes if the condition returned false
.
The syntax for an if
statement is as follows: if (<condition>) <if-body> [else <else-body]
. Let's take a look at a simple condition in Hassium:
if (7 > 3) {
println ("7 is greater than 3!");
}
Everytime you run this code, it will print 7 is greater than 3!
. This is because the condition 7 > 3
evaluates to true
, causing the if
statement to execute the if-body. Let's modify the code to something that will evaluate to false
:
if (5 < 2) {
println ("5 is less than 2");
}
This code will never print anything, since the condition 5 < 2
evaluates to false
. Now we can add an else body to this block:
if (5 < 2) {
println ("5 is less than 2");
} else {
println ("5 is NOT less than 2");
}
Since the condition 5 < 2
evaluated to false
, and there was an else
body present, the code in the else
was executed, displaying 5 is NOT less than 2
.
A loop is a type of statement where a block of code is executed repeatedly, usually while a condition is met.
The most basic form of loop is a while
loop, it executes it's body as long as it's condition keeps on returning true
. The syntax is while (<condition>) <body>
. The following is a loop that counts from 0 to 10, printing out the numbers:
counter = 0;
while (counter <= 10) {
println (counter);
counter = counter + 1;
}
This code starts by declaring a variable called counter
with the value of 0
. From there it checks to see if counter
is less than or equal to 10
, which it is. It then executes the body of the loop, printing out the counter
and then incrementing it. The condition is then checked again, and again until the counter is equal to 11
. When this happens the condition no longer returns true
and the loop will terminate.
The until
loop is the inverse of the while
loop. It executes as long as the condition returns false
, until the condition returns true
. Here is the same program from before using an until
loop:
counter = 0;
until (counter > 10) {
println (counter);
counter = counter + 1;
}
The for
loop is often used to shorten loops that fit a certain template. It includes an initialization statement that is executed once, a repeating statement that is executed each time the loop runs, and a condition that keeps the loop going. The syntax of a for
loop is: for (<initStatement>; <condition>; <repeatingStatement>) <body>
.
Take the program from the section on while
loops. It counts from 0 to 10 by iterating over a variable and incrementing it each time. This program can easily be shortened with a for
loop. The counter = 0
is the initial statement, the counter < 10
is the condition, and the counter = counter + 1
is the repeating statement. Take a look:
for (counter = 0; counter <= 10; counter = counter + 1) {
println (counter);
}
This code starts by running the inital statement, setting counter
to 0
. Then checks the condition for the first time, returning true
, from there the body is executed, printing counter
, finally the repeating statement is ran, incrementing counter
.
The foreach
loop is the most complicated (and probably the most useful) loop in Hassium. It works by behind the scenes, iterating through an object and assigning each iteration to a variable provided. The types of objects that can be iterated through (builtin) are strings, lists, tuples, dictionaries, and Stacks. The syntax for a foreach
loop is: foreach (<variable> in <iterableObject>) <body>
.
To replicate the counting program from the previous examples, we must first take a look at a global function called range
. The range
function works by taking in two ints
, one lower bound and one upper bound. From there a list
is returned with all whole integers from the lower bound to the upper bound. Try running the following program:
func main () {
println (range (2, 5)); # prints 2, 3, 4
}
Since it returns a list
, which is iterable, we can use this call as the object for our foreach
loop. Here is the counting code with a foreach loop:
foreach (counter in range (0, 11)) {
println (counter);
}
This starts by executing the range
function, returning a list
with the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
. The loop then begins to iterate over each of these numbers, assigning them to the counter
variable and executing the body, printing the number.