Loops
While loops
In a while loop, you execute a certain block of code while a condition is true. The syntax is as follows:
The example below counts from 1 to 10:
The following example prints nothing. The while loop is skipped because the condition is initially false:
Do-while loops
Do-while loops are similar to while loops, except that the block of code is executed at least once, unconditionally. It is repeated while a condition is true. Syntax:
The example below counts from 1 to 10:
The following example prints the number 20. Since the condition is false, the block of code is executed only once:
Do-while loops are available since SurgeScript 0.6.0.
For loops
A for loop is a more controlled repetition structure compared to a while loop. For loops support an initialization command, a condition and an increment command. The syntax is as follows:
The for loop above is equivalent to:
The example below counts from 1 to 10:
Since SurgeScript 0.6.1, the initialization, condition and increment expressions may be empty. Empty initialization and increment expressions do nothing. An empty condition is interpreted as true
, meaning that the loop will keep repeating until you break out of it with a break
or with a return
statement.
Foreach
Foreach loops are used to iterate throughout iterable collections (such as Arrays and Dictionaries). Basically: for each element x
in the iterable collection, do something with x
. The syntax is as follows:
The example below counts from 1 to 10:
The example below iterates over a Dictionary:
dictionary = { "a": 1, "b": 2, "c": 3 };
foreach(entry in dictionary) {
Console.print(entry.key);
Console.print(entry.value);
}
Implementing your own iterable collections
The foreach loop can be used with any iterable collections, not only Arrays and Dictionaries. You may even implement your own!
Break
Use the break
statement to terminate the innermost enclosing loop. The statement following the loop will be executed immediately afterwards. Example:
i = 0;
while(i < 10) {
if(i == 5)
break; // break out of the loop when i is equal to 5
i = i + 1;
}
// This line will be executed immediately after breaking out of the loop
Console.print(i); // Output: 5
Continue
Use the continue
statement to terminate the current iteration of the innermost enclosing loop and to start the next iteration. The exact behavior of continue
depends on the type of the loop:
- In
while
anddo-while
loops,continue
jumps to the loop condition. The loop condition is reevaluated. - In a
for
loop,continue
jumps to the increment expression. Next, the loop condition is reevaluated. - In a
foreach
loop,continue
jumps to the start of the next iteration.
Example:
i = 0;
while(i < 5) {
i = i + 1;
if(i == 3)
continue; // jump back to the condition i < 5 when i is equal to 3
Console.print(i);
}
// Expected output: 1 2 4 5
// Number 3 was skipped
A similar example with a for
loop:
for(i = 1; i <= 5; i++) {
if(i == 3)
continue; // jump to i++ when i is equal to 3
Console.print(i);
}
An analogous example with a foreach
loop: