IF / THEN, the C64 Way
Let the program decide. IF tests a condition; THEN runs the rest of the line only when it holds. The C64 has no ELSE — so two-way choices are two IFs, one per outcome.
The program can ask a question now, but it does the same thing whatever the answer. IF
changes that: it tests a condition, and THEN runs the rest of the line only when the
test is true. That single decision is what makes a program feel alive.
Milestone 1 — one test, one outcome
10 INPUT "GUESS A NUMBER 1-9";G
20 IF G=7 THEN PRINT "CORRECT!"
30 PRINT "GAME OVER"
Line 20 reads: if G equals 7, then print CORRECT!. If the test is false, the THEN
part is skipped and the program carries on. Either way, line 30 runs. RUN it and guess
7:
The test uses = to compare (is G equal to 7?), the same symbol that assigns in
LET. The C64 tells the two apart by where they sit: after IF it's a question, after a
name it's an assignment.
Milestone 2 — two-way choices, the C64 way
Most decisions have two sides — too low or too high. Some BASICs add ELSE for the
other branch. The C64 has no ELSE. You write one IF per outcome instead, and
because only the true one fires, exactly one runs:
| 1 | 1 | 10 INPUT "GUESS A NUMBER 1-9";G | |
| 2 | 2 | 20 IF G=7 THEN PRINT "CORRECT!" | |
| 3 | - | 30 PRINT "GAME OVER" | |
| 3 | + | 30 IF G<7 THEN PRINT "TOO LOW" | |
| 4 | + | 40 IF G>7 THEN PRINT "TOO HIGH" | |
| 4 | 5 | |
Three tests — equal, less-than, greater-than — cover every case. RUN it and guess 3:
< is less-than, > is greater-than; with = they are the comparisons you'll use most.
(There's also <> for not equal.) Lining up one IF per outcome is the C64's
substitute for ELSE, and it reads cleanly once you're used to it.
When it doesn't work
- Every branch printed, or none did. Check each test. With separate
IFs, two can be true only if your conditions overlap — make them exclusive (<,=,>). ?SYNTAX ERRORafter THEN.THENwants either a statement (THEN PRINT …) or a line number to jump to (THEN 100) — not nothing.- The comparison never matches. Comparing a number box to text, or vice versa, won't
match. Compare like with like — numbers to numbers,
$to$.
Before and after
You started with a program that ran the same way every time and finished with one that
branches — taking different paths for different answers. The idea underneath: IF tests,
THEN runs when true, and with no ELSE you write one IF per outcome. Decision plus
input is the core of every game rule you'll write.
Try this
- Add the win line. Keep the three-way version and add a fourth
IFthat prints a message only whenG=7. - Words, not numbers.
INPUT "YES OR NO";A$thenIF A$="YES" THEN PRINT "GREAT". Strings compare too. - Jump instead of print. Change a
THEN PRINT…toTHEN 100and add a line 100 — the test now decides where the program goes, the seed of Unit 8.
What you've learnt
IFtests a condition;THENruns the rest of the line only when it's true.=compares afterIF(and assigns after a name);<,>and<>are the other comparisons.- The C64 has no
ELSE— write oneIFper outcome, and only the true one runs.
What's next
You can branch on a test. Next you'll repeat — automatically. In Unit 7 we meet
FOR / NEXT, the counted loop that does a thing a fixed number of times without you
writing the line out again and again.