'F' → Fullscreen
Back to the terminal today. Copy template-console, not template-game.
Copy template-console to 1-15-countdown and write it:
local count = 5
while count > 0 do print("T-minus " .. count) count = count - 1end
print("Liftoff")T-minus 5T-minus 4T-minus 3T-minus 2T-minus 1LiftoffThen change it to count down from 20, and then to count up to 20. The second one needs all three parts changed — work out which three before you type.
Delete the line that changes count:
local count = 5
while count > 0 do print("T-minus " .. count)endT-minus 5T-minus 5T-minus 5T-minus 5T-minus 5...Write down what the terminal did, and how long you let it run before you stopped it. Then put the line back.
Copy the template again to 1-15-guess:
local secret = 7local guess = 0
repeat io.write("Guess my number: ") guess = tonumber(io.read())until guess == secret
print("You got it.")Guess my number: 3 [Enter]Guess my number: 9 [Enter]Guess my number: 7 [Enter]You got it.Try to rewrite this with while and see what goes wrong. You have to invent a first guess before the loop starts, and the person has not guessed anything yet.
The local guess = 0 line is there for a reason. Work out what breaks without it.
Add a tries variable that starts at 0, goes up by one inside the loop, and gets reported at the end: "You got it in " .. tries .. " tries."
This is the same shape as last session's hits. It is also the shape of every score in every program you will write this year.
Inside the loop, tell the guesser whether they were too high or too low. Two decisions inside a loop, which is the first time you have nested one of these boxes inside another.
Replace local secret = 7 with a random number between 1 and 20:
math.randomseed(os.time())local secret = math.random(1, 20)math.randomseed(os.time()) has to be there, and it has to be first. Take it out and run the program three times — write down what you notice.
This is the panel to pay attention to this quarter. The exam has two loops, and one of them is not while.
local count = 5
while count > 0 do print(count) count = count - 1endcount ← 5
REPEAT UNTIL(count = 0){ DISPLAY(count) count ← count - 1}The two loops are REPEAT n TIMES and REPEAT UNTIL(condition). That is all of them.
| Repeats | Checks | If true at the start | |
|---|---|---|---|
while c do | While c is true | Before each pass | Can run zero times |
repeat … until c | Until c is true | After each pass | Always runs once |
REPEAT UNTIL(c) | Until c is true | Before each pass | Can run zero times |
REPEAT UNTIL shares its word with Lua's repeat and its timing with while. The College Board says so directly: "if the conditional evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop."So the exact Lua translation of REPEAT UNTIL(c) is while not c do — not repeat ... until c.
The two differ only when the condition is already true at the start. Then the exam's loop does nothing and Lua's repeat runs once anyway — which is exactly the case a trace question likes to ask about.
To translate a while loop, flip the condition. while count > 0 becomes REPEAT UNTIL(count = 0). Keeping the condition the same is the single most common mistake on these questions.
REPEAT n TIMES is the other one, and it is next session — along with the fact that it does not give you a counter.
1-15-countdown counts down from 20 and up to 201-15-guess keeps asking until the guess is rightrepeat and not whileAnswer the following questions before submitting your work.
repeat. Describe a situation where a loop should be able to run zero times, and say which loop you would use for it.while count > 0 becomes REPEAT UNTIL(count = 0) in exam notation. Explain why the condition had to change, and what would happen to a program that kept it the same.Submit the required files to the appropriate dropbox.