Four short programs with loops in them, each with exactly one thing wrong. One of them will not stop.
This should add five numbers and print Total: 61. It stops with an error about a nil value instead.
local scores = {12, 8, 21, 3, 17}
for i = 1, #scores do local total = 0 total = total + scores[i]end
print("Total: " .. total)There are two things wrong here and they have the same cause. Find the cause and both go away.
Three names in the table, and it prints two of them.
local names = {"Ada", "Bo", "Cy"}
for i = 1, #names - 1 do print(names[i])endThis should count down from 3 and then say Game over. It prints Life 3 forever.
local lives = 3
while lives > 0 do print("Life " .. lives)end
print("Game over")This should keep going until x drops to 10, taking about ten passes. It takes one.
local x = 20local passes = 0
repeat passes = passes + 1 x = x - 1until x > 10
print("Passes: " .. passes)The loop itself is fine. Read the word before the condition.
For each program, give:
Count the passes on paper. For each loop, write down what the condition is at the start of every pass. Four rows is usually enough to see the problem.
Worth 5 points, graded on completion.