Back

Section Summary: Loops and Tables

divider

Activities 1.15 – 1.20. Use this to review, or to fill in something you missed.


1. Loops That Run Until Something Is True

A while loop tests its condition before each pass, so it can run zero times. A repeatuntil tests after, so it always runs at least once.

while c doend
Repeats while the condition is true.
repeatuntil c
Repeats until the condition becomes true. The body runs first.
Infinite loop
A loop whose condition never changes. The window stops responding.

Something 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.

RepeatsChecksIf true at the start
while c dowhile cbeforeruns zero times
repeatuntil cuntil cafterruns once
REPEAT UNTIL(c)until cbeforeruns 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.


2. Loops That Count

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.

Numeric for
A loop with a built-in counter. for i = 1, n do.
Loop variable
The counter. Usually i. It disappears when the loop ends.
Accumulator
A variable that builds up a result across passes. It goes above the loop, never inside.

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.


3. Nested Loops

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.


4. Tables

One name holding many values. The first element is number 1, in Lua and on the exam alike.

Table
One name holding many values. The exam calls it a list.
Element
One of the values in it.
Index
The number that picks an element. Starts at 1.
#t
How many elements. Written LENGTH(t) on the exam.

5. Two Real Differences From the Exam

Everything above is spelling. These two change answers.

LuaExam notation
An index past the endHands back nil and carries onAn error message, and the program terminates
Assigning one list to anotherA second name for the same tableA 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.

nil
Nothing is here. Not zero, not an empty string, not an error.
Alias
A second name for the same table.
Out of range
An index the table has no element for.

6. Traversal

Visiting 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 doWhen you need the index for something else. What the exam looks like.
for i, v in ipairs(t) doHands 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.