Loops
A loop is a statement which allows code to be repeatedly executed. Sanny Builder supports three kinds of controls loops: for, while, repeat.
FOR..END
The FOR
loop has a strictly certain number of iterations (repetitions).
Syntax:
FOR <loop variable> = <initial value> TO/DOWNTO <final value> [step = 1]
<the loop body>
END
<loop variable>
- a variable used as a counter for iterations
<initial value>
- a value of the loop variable before the first iteration (any value including a model identifier)
TO
or DOWNTO
- increment or decrement the loop variable between iterations
<final value>
- a value of the loop variable after the last iteration (any value including a model identifier)
<step>
- an optional value the loop variable will be incremented or decremented with between iterations. By default it is equal to 1
.
If the loop variable
is not declared with any type before the loop it gets the Integer
type. If initial value
, final value
or step
are variables, they get the same type the loop variable
has To use floating-point numbers for the initial and final values, declare the loop variable with the Float
type.
Variables $MyCounter
and $final
both have the Float
type after the loop.
WHILE..END
Syntax:
WHILE <loop condition>
<the loop body>
END
loop condition
- a single conditional opcode
loop body
- commands to execute on each iteration; can be omitted
The WHILE
loop works while the loop condition is true. The condition is evaluated before a loop iteration. Hence, if the condition is false, the loop body never gets executed.
Constants True
and False
can be used as a loop condition.
This loop body executes infinitely until the loop is stopped with the Break
command.
This loop is ignored by the compiler as the condition is never met.
Currently the compiler accepts only one opcode in the loop condition, but you can check more conditions before the loop body and use the commands Break
and Continue
.
REPEAT..UNTIL
Syntax:
REPEAT
<the loop body>
UNTIL <loop condition>
loop body
- commands to execute on each iteration; can be omitted
loop condition
- a single conditional opcode
The REPEAT..UNTIL
loop executes until the loop condition returns false. The condition is evaluated after iteration therefore the loop is guaranteed to be executed at least once.
Constants True
and False
can be used as the loop condition.
Currently the compiler accepts only one opcode in the loop condition, but you can check more conditions after the loop body and use the commands Break
and Continue
.
Continue and Break
If you want to skip the current iteration and proceed to the next one, use the Continue
command.
The Break
command causes the loop to stop immediately and proceed to the command after the loop body.
They can substitute an opcode parameter (e.g., jf Continue
) or serve as a standalone statement.
Last updated