Activities 1.15 – 1.20. Use this to review, or to fill in something you missed.
A while loop tests its condition before each pass, so it can run zero times. A repeat … until tests after, so it always runs at least once.
while c do … endrepeat … until cSomething inside the loop has to move the condition toward being false — or toward being true, for repeat. Forgetting that one line is the whole of how infinite loops happen.
The exam's REPEAT UNTIL(c) takes its word from Lua's repeat and its timing from while. It repeats until the condition is true, and it checks before each pass — so if the condition is already true at the start, the body never runs.
| Repeats | Checks | If true at the start | |
|---|---|---|---|
while c do | while c | before | runs zero times |
repeat … until c | until c | after | runs once |
REPEAT UNTIL(c) | until c | before | runs zero times |
The exact Lua translation of REPEAT UNTIL(c) is while not c do. The two only behave differently when the condition is already true at the start, which is precisely what a trace question likes to ask about.
for i = 1, 10 do gives you a counter for free. It counts from the first number to the second, including both ends, and i exists only inside the loop.
forfor i = 1, n do.i. It disappears when the loop ends.The accumulator rule turns up three times in this quarter — hits, tries, total. Declared inside the loop, it resets every pass and the answer is always the last one.
The exam has no counting loop. REPEAT n TIMES repeats but hands you no counter, so exam code that needs an index makes one by hand and increments it at the bottom of the body.
A loop inside a loop. The inner one finishes completely on every pass of the outer one, so a 3-by-4 pair of loops runs its body 12 times.
Two counters means two dimensions: row and col multiplied out into x and y is a grid.
One name holding many values. The first element is number 1, in Lua and on the exam alike.
#tLENGTH(t) on the exam.Everything above is spelling. These two change answers.
| Lua | Exam notation | |
|---|---|---|
| An index past the end | Hands back nil and carries on | An error message, and the program terminates |
| Assigning one list to another | A second name for the same table | A copy |
Both of Lua's behaviors are the quieter one, which is exactly what makes them dangerous. A nil travels and fails somewhere else; an alias means changing one table changes the other and nothing says so.
nilVisiting every element in order. The exam's word, and it appears constantly — almost everything anyone does with a list is a traversal with one line changed in the middle.
for i = 1, #t do | When you need the index for something else. What the exam looks like. |
for i, v in ipairs(t) do | Hands you the index and the value together. No ipairs on the exam. |
Because the loop takes its length from the table, adding a value changes no code.
Parallel lists are two tables read with the same index — xs[3] and ys[3] being one circle. Nothing but the index connects them, and Lua will never check that they are the same length.
A mismatch fails in opposite directions. If the list being walked is the longer one, you get nil and a crash. If it is the shorter one, nothing happens at all — and that is the worse outcome, because it ships.
A gap in a table is not a list. Both #t and ipairs stop at the hole, so an element after it is in the table and unreachable.