Skip to content

Flow Control Statements

Sometimes we need Modulo1 to do something other than just read our code from top to bottom. We might need it to make a decision before deciding what to do next, or we might need it to perform a set of instructions repeatedly. The way to achieve this, is through flow control statements.

Modulo1 includes the same kinds of flow control statements that exist in most other programming languages:

  • while loop statements
  • do/while loop statements
  • for loop statements
  • switch statements
  • if statements

Each one of these statements contains one or more conditional expressions, which determine what decision should be made. Additionally, each one of these statements contains one or more blocks of code that may or may not be executed depending upon the results of the expressions.


While Statements

A while statement will repeat its block of code so long as the specified condition remains true.

Here is the simplest possible example of a while statement:

1
while (true) { };

The syntax begins with the while keyword, followed by the condition, which will always be true in this case because there is no calculation being performed that might have changed the condition.

Another issue is that this particular while statement has no code inside its code block. Under normal circumstances it should have code inbetween the curly braces: {}. This while statement will run forever (or until Modulo1 realises that nothing is happening and then shuts down the code).

Here is a better usage of a while statement:

1
2
3
4
5
6
7
var count = 0;

while (count < 3) {
    var result = 10 * count;
    print(result);
    count += 1;
}

It will print the following:

1
2
3
0
10
20

In this example, we create a new variable called count before our while statement, so that we can use this variable as part of the while statement's condition. Modulo1 will evaluate the condition before each loop cycle. And if the result is ever false, the while loop will come to an end.

The code in this block will increase the count variable by one, every time the loop is run. So, after three loops the condition will fail, and the while loop will stop.

It is also possible to manually stop a while loop, rather than waiting for the condition to become false:

1
2
3
4
5
6
7
8
var count = 0;

while (count < 3) {
    var result = 10 * count;
    print(result);
    count += 1;
    break;
}

It will print the following:

1
0

The reason is because we included the break keyword. This tells Modulo1 that we want to stop immediately.


Do/While Statements

The do/while statement is almost identical to the while statement. The only real difference, is that the do/while statement evaluates the condition after the loop, which means that it will always run at least once.

Here is an example:

1
2
3
do {
    print("Woops!");
} while (false);

It begins with the do keyword, followed by the code block between curly braces {}, then the while keyword and our condition. In this case our condition will always be false, so the loop will never repeat.

It will print the following:

1
Woops!

For Loops

The for loop runs a block of code a limited number of times. The amount of times it runs, however is not based on a true/false kind of condition. Instead, the loop is controlled by a special kind of variable called an iterator.

An iterator acts like a kind of middleman between itself and a complex variable such as an object or an array. The iterator does not hold a value, but simply 'points' to a value inside the complex variable. So that when your code tries to access the non-existent value inside the iterator, Modulo1 will instead seek the value that the iterator is pointing to. It is the iterator's job to make sure that it is pointing to a new value every time that Modulo1 asks for the value to be updated, which happens to be at the end of each for loop cycle. When the iterator runs out of items to point to, inside the complex variable, the for loop will end.

Here is an example:

1
2
3
4
5
var myArray = [2, 3, 7, 2, 5];

for (var num in myArray) {
    print(num);
}

It will print out each number in myArray and then stop:

1
2
3
4
5
2
3
7
2
5

Info

For more information, check out our page on iterators


Switch Statements

The switch statement is not a loop. It uses a variable-matching kind of condition to choose which code should be run. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var value = doSomething();

switch (value) {
    case 1:
        print("bear");
        break;
    case 2:
        print("lion");
        break;
    default:
        print("none");
}

You should interpet the above code as basically saying:

Interpretation

Here's a variable called value. If it equals 1 then print the word "bear", then stop. But if that was not a match, then check if the variable value equals 2. If so, then print the word "lion", then stop. Since we haven't stopped yet, then just print "none".

We start by creating a new variable called value and using that as the input to our switch statement.

Next, Modulo1 will compare the input to each of the cases in the switch statement. If it finds a match, then it will execute the code that follows that case. If you forget to write break at the end of the code (or something else that stops the code such as a return statement), Modulo1 will assume that you don't want it to stop and it will keep trying to find matches for the case.


If Statements

The if statement is also not a loop. But it does use true/false kinds of conditions to determine which code should be run. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var value = doSomething();

if (value == null) {
    print("Why is this null?");
} else if (value == 0) {
    print("We failed");
} else if (value > 10) {
    print("Great score");
} else {
    print("That's OK");
}

You should interpet the above code as basically saying:

Interpretation

Here's a variable called value. If it equals null, then print the text "Why is this null?". If not, then if it equals 0 print the text "We failed". Otherwise, if it is greater than 10 then print the text "Great score". If not, just print the text "That's OK".