Part I · Getting It Running
Chapter 4: Doing It Again
Chapter 3 left your player with exactly one shot at the riddle. Get it wrong, and the game just... stops. Real games give you another try. This chapter is about running the same code more than once, on purpose, and knowing how to stop.
The while Loop
while repeats a block of statements for as long as a condition stays
true. It checks the condition before every single pass, including the
first one.
var attemptsLeft = 3
while attemptsLeft > 0
writeln("Attempts left: " + attemptsLeft)
attemptsLeft = attemptsLeft - 1
end whileThat prints three lines and stops on its own, because eventually
attemptsLeft > 0 becomes false. A while loop is only as good as its
condition. If the condition never becomes false, the loop never stops.
An Infinite Loop, on Purpose
Let's actually watch that happen, safely. Try running this:
var x = 1
while x > 0
writeln(x)
x = x + 1
end whilex starts at 1 and only ever gets bigger, so x > 0 is never false.
Left alone, this would run forever and freeze your browser tab. It won't,
though. After 10,000 completed iterations, QF Code stops the loop itself
and shows you a runtime error pointing at the loop, along with a hint
about what to check, your condition, your range, or your step value.
This isn't QF Code being fussy. It's a safety net. Every beginner writes a loop that doesn't end, usually by accident, usually within their first few tries. The 10,000-iteration limit exists so that mistake costs you a glance at an error message instead of a frozen tab. You'll almost certainly hit this limit for real at some point while working through this book. When you do, you'll know exactly what happened and why.
There's a second, related limit worth knowing about now too: a whole program is limited to 100,000 total execution steps, not just loop iterations. That one catches the cases a single loop's counter wouldn't, like a function that keeps calling itself without ever stopping. You won't run into it as often, but it exists for the same reason.
Fixing the Loop Above
The fix for the infinite loop is almost always the same: make sure something inside the loop actually moves the condition toward false.
var x = 1
while x <= 5
writeln(x)
x = x + 1
end whileSame shape, but now x is climbing toward a ceiling instead of climbing
forever, and the loop stops on its own once x passes 5.
break and continue
Two keywords let you interrupt a loop from the inside. break exits the
loop immediately, no matter what the condition says. continue skips the
rest of the current pass and jumps straight to the next condition check.
var tries = 0
while tries < 3
tries = tries + 1
var guess = input("Guess the number (1 to 10): ")
if num(guess) == 7 then
writeln("Correct!")
break
end if
writeln("Nope, try again.")
end whilebreak is what lets a loop end early because something good, or bad,
happened, instead of only ending when the counter runs out.
for and for each
When you already know exactly how many times to repeat something, for
is usually a better fit than while.
for i = 1 to 5
writeln("Count: " + i)
end forThat runs exactly five times, i taking each value from 1 to 5 in
turn. for loops are capped at 10,000 iterations too, for the same
reason while is.
for each is for stepping through every item in something you already
have, like an array or a string, without managing a counter yourself.
You haven't met arrays yet, that's chapter 5, but here's a preview using a
string:
for each letter in "hello"
writeln(letter)
end forGrowing the Adventure
Now the riddle gets real stakes: three attempts, not one.
// The Hollow Cottage
// Chapter 4
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var tries = 0
var solved = false
while tries < 3 and not solved
tries = tries + 1
var answer = input("The door asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
solved = true
else
writeln("Nothing happens.")
end if
end while
if not solved then
writeln("The door refuses to budge. You'll have to find another way in.")
end ifMake It Yours. Change the number of attempts, or add a break
somewhere it makes sense, or use a for loop instead of while if you
can figure out how to make it fit. There's more than one correct way to
write this.
Chapter Checkpoint
Open either known-good checkpoint directly in QF Code, or download the .qfc file to keep locally. The IDE opens in a new tab and does not run the program automatically.
Start this chapter
Chapter 4 starting checkpoint.
Continue from here
Chapter 4 completed checkpoint.
Starting code: the Chapter 3 checkpoint.
Completed code (Chapter 4):
// The Hollow Cottage
// Chapter 4
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var tries = 0
var solved = false
while tries < 3 and not solved
tries = tries + 1
var answer = input("The door asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
solved = true
else
writeln("Nothing happens.")
end if
end while
if not solved then
writeln("The door refuses to budge. You'll have to find another way in.")
end ifCopyright © 2026 Edison Mooers. Published by Gibidda Press. Free for personal learning and qualifying noncommercial educational use. Commercial training, organizational use, redistribution, and adaptation require written permission. Full use terms.