Loops
Loops in TinyPanda allow you to execute blocks of code repeatedly.
loop (While Loop)
The loop statement acts as a traditional while loop. It repeats its block statements as long as its condition expression evaluates to true.
Syntax
loop (condition) {
// statements to execute
}
NOTE
The body of a loop must always be enclosed in curly braces { }, even if it only contains a single statement.
Usage
To run a loop, initialize a control variable, set a termination boundary in the condition, and update the variable inside the loop body.
bamboo x = 1;
loop (x <= 3) {
echoln("The value of x is " + str(x));
x++;
}
// Output:
// The value of x is 1
// The value of x is 2
// The value of x is 3
for Loop
The for statement provides a clean way to write counter-controlled loops.
SYNTAX CONSTRAINT
All loop variables must be declared in the outer scope before the loop begins.
Syntax
for (initializer; condition; iteration) {
// statements to execute
}
initializer: A standard assignment expression (e.g.,x = 0) that sets the starting value of an already declared variable.condition: An expression checked before each iteration. The loop runs as long as this evaluates to true.iteration: An update statement executed at the end of every loop iteration (typically postfix operators likex++orx--, or assignment statements).
Usage
To run a for loop, declare your iteration variable outside first, and then initialize and increment it inside the loop header:
bamboo i = 0;
for (i = 0; i < 5; i++) {
echoln(i);
}
// Output:
// 0
// 1
// 2
// 3
// 4