For-Loop
For-loops are statements with a loop-expression, a loop condition and a statement body that are executed as long as a specified condition is met.
For-loops have the unique attribute, unlike while-loops and do-while-loops,
of having an executable LOOP_EXPRESSION
, which is evaluated at the end of every loop cycle. This LOOP_EXPRESSION
can
be used to do repeating tasks at the end of a loop cycle, like calling a function or increasing a counter.
Scheduled for release in Kipper v0.11.0
Syntax
for (INIT_EXPRESSION (OPTIONAL); CONDITION (OPTIONAL); LOOP_EXPRESSIONS (OPTIONAL)...) STATEMENT;
Execution Schema
- Evaluate
INIT_EXPRESSION
, if it exists (Only the first time). - Check
CONDITION
, if it exists, before running theSTATEMENT
. IfCONDITION
isfalse
, then the loop will be stopped! - Run
STATEMENT
ifCONDITION
wastrue
. - If the loop was not stopped using
return
orbreak
, evaluateLOOP_EXPRESSION
after finishing the execution ofSTATEMENT
.
Examples
// ✓ Simple for-loop with an execution counter
for (var i: num = 1; i < 10; i++) {
call print(f"Running for the {i}. time!");
}
// ✓ Simple for-loop with two execution counters
var j: num = 0;
for (var i: num = 1; i < 10; i++, j++) {
call print(f"i = {i}");
}
call print(f"Additional variable j = {j}"); // -> Additional variable j = 10
// X Infinite loop - Avoid this, as it results in your program freezing/running forever
for ( ; ; ) {
call print(f"Running for the {i}. time!");
}