Loops - markpwns1/Dormez GitHub Wiki

Dormez has four kinds of loops:

While loops

declare i = 0;
while i < 10 {
    console.print(i);
    i++;
}

Until loops

Until loops are just while loops but with the condition inverted. This until loop, for example, is equivalent to the while loop shown above.

declare i = 0;
until i == 10 {
    console.print(i);
    i++;
}

From loops

These are kind of like for loops in other languages, and are meant to be a shortcut for iterating over ranges of numbers. The following code is equivalent to the two loops shown above:

from 0 to 10 with i {
    console.print(i);
}

The upper limit is exclusive, meaning i will never actually reach 10. You can also specify an increment using the by keyword. To go up by three, for example, use this:

from 0 to 10 by 3 with i {
    console.print(i);
}

This would output 0, 3, 6, and 9 on new lines.

The lower bound, upper bound, and increment can be evaluated, so they do not have to be constant numbers. Refer to the following example, showing a legal from loop:

declare beginning = 0;
declare end = 10;
declare increment = 1;

// legal
from beginning to end by increment with index {
    console.print(index);
}

// also legal
// from 9 to -1 by -1 with i
from math.sqrt(80 + 1) to (1-1)^3 - 1 by math.nthRoot(-1, 3) with i {
    console.print(i);
}
// Keep in mind that the 'upper bound' (-1) is exclusive, so it will iterate from 9-0

For-each loops

These are like the foreach loops from C#. The following loop prints each number in 'set', which should be the numbers from 0 to 9.

declare set = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
for each number in set {
    console.print(number);
}

Break and continue

You can use break and continue in loops just like in other programming languages. The following example will print every number from 0 to 9 inclusively, except for 5.

from 0 to 10 with i {
    if i == 5 {
        continue;
    }
    console.print(i);
}

The following function will print every number from 0 to 4 inclusively:

from 0 to 10 with i {
    if i == 5 {
        break;
    }
    console.print(i);
}