Part I · Getting It Running
Chapter 3: Making Decisions
Your game has been saying "the door is locked" since chapter 2, but it's been lying. Nothing has actually checked whether it should be locked. Every program you've written so far runs the same way no matter what the player does. That changes now.
The if Statement
if lets your program choose whether to run a block of statements at all.
var hasKey = false
if hasKey then
writeln("You unlock the door and step inside.")
end ifRun that with hasKey set to false, and nothing happens, the block
between if and end if is skipped entirely. Set it to true, and the
line runs. if always needs a matching end if to mark where its block
stops.
Comparison Operators
Most conditions come from comparing two values:
== // equal to
!= // not equal to
< // less than
> // greater than
<= // less than or equal to
>= // greater than or equal tovar age = 12
if age >= 13 then
writeln("You may enter the tavern.")
end ifA few rules worth knowing before they surprise you. First, == compares
strings case-sensitively, "Key" and "key" are not equal. Second, <,
>, <=, and >= require both sides to be the same type, you can
compare two numbers or two strings, but comparing a number to a string
directly is an error, not an automatic conversion. If you ever need to
compare a number the player typed in against a string, convert it first
with num(), the same way you did back in chapter 2.
Else and Else If
if on its own only handles one path. else gives you a fallback, and
else if lets you check additional conditions in order.
if hasKey then
writeln("You unlock the door and step inside.")
else if knowsPassword then
writeln("You whisper the password. The door creaks open.")
else
writeln("The door stays shut.")
end ifQF Code checks each condition top to bottom and runs only the first branch that matches. Once one branch runs, the rest are skipped, even if they would also have been true.
Combining Conditions
and, or, xor, and not let you build a single condition out of
several smaller ones.
if hasKey and knowsPassword then
writeln("Overkill, but the door opens immediately.")
end if
if hasKey or knowsPassword then
writeln("One is enough. The door opens.")
end if
if not hasKey then
writeln("You'll need to find a key.")
end ifand and or short-circuit, meaning and stops checking as soon as one
side is false, and or stops as soon as one side is true. xor is
different: it always checks both sides, and it's true only when exactly
one of them is.
One thing to unlearn if you've dabbled in other languages before: QF Code
won't let you treat a string or a number like 5 as a stand-in for true
or false. Logical operators only accept actual booleans, or the numbers
0 and 1. Anything else, a string, an array, empty, raises an error
rather than quietly guessing what you meant.
Growing the Adventure
Time to make the door actually respond to the player.
// The Hollow Cottage
// Chapter 3
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
else
writeln("Nothing happens. The door stays shut.")
end ifRun it, answer correctly, answer wrong, watch both paths actually behave differently. That's the whole idea of this chapter, made real.
Make It Yours. Write your own riddle, or replace it with a password,
a choice between two doors, or anything else where the player's answer
should change what happens next. Give it at least one else or
else if branch, not just a single if.
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 3 starting checkpoint.
Continue from here
Chapter 3 completed checkpoint.
Starting code: the Chapter 2 checkpoint.
Completed code (Chapter 3):
// The Hollow Cottage
// Chapter 3
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
else
writeln("Nothing happens. The door stays shut.")
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.