Repeat until

This is a similar kind of loop to the while loop, except the condition for the loop ending is placed at the end of the loop, which means that the loop will always run at least once. This program demonstrates a repeat loop being used.

exitProgram as integer = 0

repeat print ( "inside a repeat loop" )
sync ( ) until exitProgram = 1

To start this loop use the repeat keyword. Anything placed after it up to the until keyword will form part of the loop. The condition to end the loop must be placed directly after the until keyword. This loop is useful if you do not know the condition to end the loop until the loop code has been executed.

This program shows a repeat loop being used to add 1 to a variable named counter. It will exit the loop when this variable reaches the value 10. This is followed by a do loop that is used to print the value of the counter variable on the screen.

counter as integer = 0

repeat counter = counter + 1 until counter = 10
do print ( counter )
sync ( ) loop

It's important to remember that the loop will always execute at least once, as demonstrated in this program.

counter as integer = 10

repeat counter = counter + 1 print ( counter ) sync ( ) until counter = 10
do print ( counter ) sync ( ) loop

The variable counter is assigned a value of 10 and then the repeat loop is declared with a condition to exit when counter has a value of 10. However, because the code preceeding it will be executed before the condition is met then by the time the condition is checked counter will have had 1 added to it and have a value of 11, therefore the exit condition will not be met and the loop will continue around. In this particular example the loop will continue infinitely as the exit condition will never be met.