WHILE ​
Execute and repeat a part of code while a condition evaluates to true.
Syntax ​
WHILE Condition {
# Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).
Parameters ​
Condition
(boolValue) - An expression that is evaluated and whose resulting value is interpreted to be either true or false. This is the<main-parameter>
of the corresponding command.
Details ​
The WHILE
command allows to repeat a set of commands as long as a condition evaluates to true. The condition is specified via the <main-parameter>
and should be an expression. If it evaluates to true, then the commands in the body (between the curly brackets {}
) are executed and then returns to the beginning of the loop checking the condition again.
Make sure that the body of the loop modifies the variables used in the condition to some degree, so that the condition will evaluate to false in a finite number of runs. Otherwise an endless loop is created and the application will break, as they are not automatically aborted by ADOxx.
In most cases the same can be achieved with a FOR
loop, which has the benefit that it will only run a finite number of times. It is therefore recommended to use FOR
where possible. A WHILE
is necessary if a more sophisticated condition is necessary whose variables change as part of the loop body.
These commands can be used in combination with the commands that set variables, like AVAL or BITMAPINFO, to realize different graphical representations for different objects of the same class.
See Also ​
Examples ​
Draw five darkgray squares, descending towards the bottom-right.
GRAPHREP
SHADOW off
# Parameters to easily change the size and amount of squares to draw.
SET dSquareSize:1cm
SET nSquareAmount:5
PEN color:"darkgray"
FILL color:"white"
SET dSquareOffset:(dSquareSize / 2)
SET nSquareNumber:0
WHILE (nSquareNumber < nSquareAmount) {
RECTANGLE x:(nSquareNumber * dSquareOffset) y:(nSquareNumber * dSquareOffset) w:(dSquareSize) h:(dSquareSize)
SET nSquareNumber:(nSquareNumber + 1)
}
Draw many small circles along a sinus curve, each one in a different color hue.
GRAPHREP
SHADOW off
SET nX:-3.14 # Setting to roughly the value of pi.
WHILE (nX < 3.14) {
# Using the nX variable to set the "hue" in the HSV color space.
PEN color:(hsv2rgb(hsvval(((nX + 3.14) / 6.28) * 359, 1, 0.9)))
# Calculating the y position using the sinus function (+y in GraphRep is different from typical mathematical y-axis).
ELLIPSE x:(CM nX) y:(CM sin(nX)) rx:0.2cm ry:0.2cm
# Increasing variable nX, which will also lead to laving the while loop at some point.
SET nX:(nX + 0.1)
}
# In total the loop is executed 63 times.
Versions and Changes ​
Available since ADOxx 1.3