Four problems on iteration. The exam has two loops and no while, and both of them behave in a way that a Lua habit gets wrong. This page is where you find that out on paper instead of in May.
Do this on paper. There is nothing to run and nothing to type. On exam day you get this notation on a printed sheet and no computer, so practicing it any other way practices the wrong thing.
Everything you need for this page. This is the same notation the College Board uses on the exam.
a ← expression assign a copy of the result to aDISPLAY(expression) show the value, FOLLOWED BY A SPACE
= ≠ > < ≥ ≤ comparison. = means EQUALSNOT / AND / OR boolean operators
IF(condition) no ELSE IF exists - nest an IF inside an ELSE{ ... }ELSE{ ... }
REPEAT n TIMES NO COUNTER VARIABLE IS PROVIDED{ ... } if you need one, you make it yourself
REPEAT UNTIL(condition) repeats UNTIL the condition is true{ ... } THE CONDITION IS CHECKED BEFORE EACH PASS, so the body can run ZERO timesRead the last four lines twice. REPEAT UNTIL looks like Lua's repeat … until and it is not the same loop. Lua's runs the body and then checks. The exam's checks and then, maybe, runs the body.
while c do -- checks BEFORE. can run zero timesrepeat ... until c -- checks AFTER. ALWAYS runs onceREPEAT UNTIL(c) -- checks BEFORE. can run zero timesREPEAT UNTIL(c) is while not c do — not repeat … until c. Same words, different loop.What does this display?
count ← 0
REPEAT 4 TIMES{ count ← count + 3}DISPLAY(count)Then answer this: add a line inside the loop that displays which pass you are on — pass 1, pass 2, and so on. Say what you had to add outside the loop to make that possible, and why.
Look at the starting value before you start counting. What does this display?
x ← 20steps ← 0
REPEAT UNTIL(x ≥ 10){ x ← x + 1 steps ← steps + 1}DISPLAY(steps)Line for line, this is problem 2 written with Lua's repeat. What does it print?
local x = 20local steps = 0
repeat x = x + 1 steps = steps + 1until x >= 10
print(steps)Problems 2 and 3 do not have the same answer. Say what the difference is in one sentence, then write the while loop that would give Lua the exam's answer instead.
Rewrite this Lua loop in exam notation.
for i = 1, 3 do print(i * 5)endYour version will be longer than three lines. REPEAT n TIMES hands you no i, so if the body needs one, it is yours to create, yours to use, and yours to increase.
This is the widest gap between Lua and the reference sheet in the whole quarter, and it is the reason exam questions about loops so often have a counter variable sitting above them doing nothing obvious.
Your four answers, the added lines from problem 1, and the while loop from problem 3. For the traces, write the output exactly as it appears, including spacing.
Worth 3 points, graded on completion. Show your working for the traces — a wrong answer with visible reasoning is worth more to both of us than a right one with none.