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.

QF Code example 1
var attemptsLeft = 3

while attemptsLeft > 0
    writeln("Attempts left: " + attemptsLeft)
    attemptsLeft = attemptsLeft - 1
end while

That 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:

QF Code example 2
var x = 1

while x > 0
    writeln(x)
    x = x + 1
end while

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

QF Code example 3
var x = 1

while x <= 5
    writeln(x)
    x = x + 1
end while

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

QF Code example 4
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 while

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

QF Code example 5
for i = 1 to 5
    writeln("Count: " + i)
end for

That 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:

QF Code example 6
for each letter in "hello"
    writeln(letter)
end for

Growing the Adventure

Now the riddle gets real stakes: three attempts, not one.

QF Code example 7
// 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 if

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

Starting code: the Chapter 3 checkpoint.

Completed code (Chapter 4):

QF Code example 8Open in QF Code
// 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 if

Next: once the player is inside, they're going to need somewhere to put things. Chapter 5 gives your game its first collection, a real inventory.


Copyright © 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.