Repeat until loop

A repeat until loop is slightly different from a while loop. When using a while loop, the initial expression is evaluated first, so the following code would not execute the chunk of code belonging to the loop:

while false do
print ("Not going to print")
end

This happens because the Boolean condition of the loop is evaluated before the chunk of code is executed. A repeat until loop works the opposite way. The chunk of the repeat loop is executed first, then the condition is evaluated. This guarantees that a repeat until loop will execute its chunk at least once.

Syntactically, the repeat until loop begins with the repeat keyword, followed by the chunk of code to loop. The chunk ends with the until keyword, which is followed by the logical expression to evaluate which determines if the loop should execute or not. The following piece of code demonstrates a repeat until loop that executes once but never actually loops:

x = 10
repeat
print ("Repeat loop")
until x > 2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.204.5