next up previous contents index
Next: SNNS Function Calls Up: Control Structures Previous: The For Instruction

While and Repeat Instructions

The while and the repeat instructions differ from a for instruction because they don't have a count variable and execute their commands only while a condition is met (while) or until a condition is met (repeat). The condition is an expression which delivers a boolean value. The formats of the while and the repeat instructions are:

while EXPRESSION do BLOCK endwhile

The user has to make sure that the cycle terminates at one point. This can be achieved by making sure that the EXPRESSION delivers once the value `TRUE' in case of the repeat instruction or `FALSE' in case of the while instruction. The for example from the previous section is equivalent to:

i := 2
while i <= 5 do
print ( "here we are: ",i)
i := i + 1 endwhile

or to:

i := 2
print ( "here we are: ",i)
i := i + 1
until i > 5

The main difference between repeat and while is that repeat guarantees that the BLOCK is executed at least once. The break and the continue instructions may also be used within the BLOCK.
Tue Nov 28 10:30:44 MET 1995