Skip to content

If, Else and Loops

C272 edited this page Aug 19, 2019 · 8 revisions

If Statements

"If" statements can be defined with either a single if block, an if block and else if blocks. Both of these can have an "else" block appended to them. As an example, here's a quick example.

let b = 3;

if (b == 4) {
    print "B is 4.";
}
else if (b == 3) {
    print "B is 3.";
}
else {
    print "I don't know what B is!";
}

Inside the brackets of the if/else if blocks are checks, which (if passed) will cause the statements inside the block to execute. For more information on that, see the checks and conditionals article.

"For" Loops

For loops are typically used to enumerate over arrays, or repeat a statement a number of times. If you have an existing array, you can use a for loop to enumerate over it. For loops will also take literal arrays. Here is an example of all of those types.

//Enumerating an existing list.
let array = [0, 1, 2, 3, 4];
for (i in array) {
    print array[i];
}

//Enumerating for a range. (This starts at "0", and is inclusive of the end number)
for (i up to 5) {
    print i; //0, 1, 2, 3, 4, 5
}

//Enumerating a literal array.
for (i in [0, 1, 2, 3]) {
    let x = [4, 5, 6, 7];
    print x[i];
}

The given index name in a for loop begins at zero, and ends at the index corresponding to the last element of the array. For literal ranges, the index loops inclusive of the given end number.

You can also create a "foreach" loop, which instead of providing the index for each loop of the block, provides the actual value. Here's an example of using that, versus the char list one above.

Normal for Loop

//Enumerating an existing list.
let array = [0, 1, 2, 3, 4];
for (i in array) {
    print array[i];
}

foreach Loop

//Enumerating an existing list.
let array = [0, 1, 2, 3, 4];
foreach (i in array) {
    print i;
}

These have the same output, but the foreach loop requires less work to get the value in the array, since the variable i is already set to that value at the start of each loop.

"While" Loops

While loops are used to complete an action until a condition is broken. For example, looping a specific number of times, or until the user clicks "Quit". A while block contains a check within brackets. When broken, the code inside the while block will not run again. If that condition is still true, the while block will run until it is not.

let x = 0;
while (x != null & x <= 5) {
    print x;
    x++;
}

print x; //x == 6

For more information on checks, refer to this article on checks and conditionals.

Breaking

To stop a loop while it's running, you use a return statement to return a value, or a break statement to simply stop the loop. Here is an example of both below:

//Break with return statement.
for (i up to 3) {
    print i;
    return; //only prints "0"
}

//Break with break statement.
for (i up to 3) {
    break;
}

Continuing

Instead of completely stopping a loop, you can also skip forward to the next iteration of the loop. So, any statements afterwards in the loop body will not be run, and the loop statements will be run from the start. To put it into context, here is an example:

for (i up to 10) 
{
    if (i < 5) {
        continue;
    }

    print i + " is bigger than or equal to 5!" //doesn't run the first 5 times (0-4)
}