Back

Activity 1.15: Doing It Again

divider

Activity 1.15

Doing It Again

Key Concepts

Repeating While Something Is True

Repeating Until Something Is True

Loops That Never Stop

You Already Know How to Do This Badly

print("T-minus 5")
print("T-minus 4")
print("T-minus 3")
print("T-minus 2")
print("T-minus 1")
print("Liftoff")

You Already Know How to Do This Badly

print("T-minus 5")
print("T-minus 4")
print("T-minus 3")
print("T-minus 2")
print("T-minus 1")
print("Liftoff")

Six lines for five numbers. Now count down from a hundred.

A Fourth Box

while condition do
end

Run this block, then check again. Keep going as long as the condition is true.

It looks like an if. The difference is that an if asks once.

Counting Down

local count = 5
while count > 0 do
print("T-minus " .. count)
count = count - 1
end
print("Liftoff")

Counting Down

local count = 5
while count > 0 do
print("T-minus " .. count)
count = count - 1
end
print("Liftoff")
Terminal window
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
Liftoff

Three Things Every Loop Needs

A starting value
count begins at 5.
A condition
count > 0, checked before every pass.
Something that changes
count = count - 1, or it never ends.

Miss the third and the loop is not wrong. It is forever.

Forever Is a Real Thing

local count = 5
while count > 0 do
print("T-minus " .. count)
end

Forever Is a Real Thing

local count = 5
while count > 0 do
print("T-minus " .. count)
end
Terminal window
T-minus 5
T-minus 5
T-minus 5
T-minus 5
T-minus 5
...

No error. It is still going. Stop it with the trash-can button at the top of the terminal panel.

The Other One Checks Afterwards

repeat
until condition

repeat runs the block first, then checks, and stops when the condition becomes true.

Both words are backwards from while. That is not an accident and it matters in May.

Which Means It Always Runs Once

local m = 100
repeat
print("runs once anyway, m is " .. m)
until m > 0

Which Means It Always Runs Once

local m = 100
repeat
print("runs once anyway, m is " .. m)
until m > 0
Terminal window
runs once anyway, m is 100

m > 0 was true before the loop started, and it still printed.

Which One, Then?

while
When the block might need to run zero times.
repeat
When it must run at least once — asking a question you have not asked yet.

You cannot check a guess before the person has guessed.

Today's Objectives

  • Write a while loop that counts
  • Name the three parts every loop needs
  • Stop a program that will not stop
  • Use repeat for something that must happen once

Key Terms

Loop
A block that runs more than once.
Iteration
One pass through a loop. The exam's word for looping.
while
Checks before each pass. Repeats while the condition is true.
repeat / until
Checks after each pass. Repeats until the condition is true.
Infinite loop
A loop whose condition never changes. It does not stop on its own.

'F' → Fullscreen

divider

Build

Back to the terminal today. Copy template-console, not template-game.

Task 1: Count Down

Copy template-console to 1-15-countdown and write it:

main.lua
local count = 5
while count > 0 do
print("T-minus " .. count)
count = count - 1
end
print("Liftoff")
Terminal
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
Liftoff

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

Task 2: Break It on Purpose (Required)

Delete the line that changes count:

main.lua
local count = 5
while count > 0 do
print("T-minus " .. count)
end
Terminal
T-minus 5
T-minus 5
T-minus 5
T-minus 5
T-minus 5
...
To stop it, press the trash-can button at the top of the terminal panel — its tooltip says Kill Terminal. The Run button will not help, Escape will not help, and closing the file will not help. Ctrl+C sometimes works and sometimes does nothing at all, so do not rely on it. Learn this now, while you are doing it on purpose.

Write down what the terminal did, and how long you let it run before you stopped it. Then put the line back.

Task 3: Repeat Until

Copy the template again to 1-15-guess:

main.lua
local secret = 7
local guess = 0
repeat
io.write("Guess my number: ")
guess = tonumber(io.read())
until guess == secret
print("You got it.")
Terminal
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.

Task 4: Count the Guesses

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.

Challenge (Optional): Hotter and Colder

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.

Challenge (Optional): Let the Computer Pick

Replace local secret = 7 with a random number between 1 and 20:

main.lua
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.

divider

Same Idea, Exam Notation

This is the panel to pay attention to this quarter. The exam has two loops, and one of them is not while.

Lua
local count = 5
while count > 0 do
print(count)
count = count - 1
end
Exam Notation
count ← 5
REPEAT UNTIL(count = 0)
{
DISPLAY(count)
count ← count - 1
}

There is no WHILE on the exam sheet

The two loops are REPEAT n TIMES and REPEAT UNTIL(condition). That is all of them.

RepeatsChecksIf true at the start
while c doWhile c is trueBefore each passCan run zero times
repeat … until cUntil c is trueAfter each passAlways runs once
REPEAT UNTIL(c)Until c is trueBefore each passCan run zero times
Read that bottom row twice. The exam's 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.

divider

Checkpoint

  • 1-15-countdown counts down from 20 and up to 20
  • You made a loop run forever and stopped it yourself, and you know which button does it
  • 1-15-guess keeps asking until the guess is right
  • It reports how many tries it took
  • You can say why this one is repeat and not while
divider

Reflection

Answer the following questions before submitting your work.

  1. Name the three parts every loop needs, and say which one you deleted in task 2. Explain why deleting it produced no error message.
  2. The guessing loop must ask at least once, so it uses repeat. Describe a situation where a loop should be able to run zero times, and say which loop you would use for it.
  3. 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.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete