Control Flow Details
Repeat Until
repeat ... until runs the body before checking the stop condition.
condition last
A repeat loop always runs at least once because the condition comes after the body.
Repeat Until
repeat_until.lua
Replay: real traced execution (multi-file project)
local target = 3
local count = 0
repeat
count = count + 1
until count >= target
print("target=" .. target)
print("count=" .. count)
local target = 1
local count = 0
repeat
count = count + 1
until count >= target
print("target=" .. target)
print("count=" .. count)
local target = 5
local count = 0
repeat
count = count + 1
until count >= target
print("target=" .. target)
print("count=" .. count)
target ← 3, count ← 0
1local target→ 3 = 3 --@target=1, 52local count→ 0 = 0count ← 1
pass 1 of 34repeat5 count→ 1 = count + 16until count >= targetAll 3 passes — pass 1 is the card above pass count1 0 → 1 2 1 → 2 3 2 → 3 print("target=" .. target)
8print("target=" .. target3)9print("count=" .. count3)outputtarget=3 count=3
target ← 1, count ← 0
1local target→ 1 = 12local count→ 0 = 0count ← 1
4repeat5 count→ 1 = count + 16until count >= targetprint("target=" .. target)
8print("target=" .. target1)9print("count=" .. count1)outputtarget=1 count=1
target ← 5, count ← 0
1local target→ 5 = 52local count→ 0 = 0count ← 1
pass 1 of 54repeat5 count→ 1 = count + 16until count >= targetAll 5 passes — pass 1 is the card above pass count1 0 → 1 2 1 → 2 3 2 → 3 4 3 → 4 5 4 → 5 print("target=" .. target)
8print("target=" .. target5)9print("count=" .. count5)outputtarget=5 count=5
Repeat Until Target
targetstarts at3.countstarts at0.- The repeat body runs and adds
1. - The loop stops when
count >= target. - The final count is
3. | Pass | Count after pass | Stop? | | --- | --- | --- | |1|1| no | |2|2| no | |3|3| yes |
Exercise: repeat_until.lua
Use repeat until to count up until count reaches the target