Skip to content
Game 0 Unit 6 of 15 1 hr learning time

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.

40% of Meet C64 BASIC

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 C64 screen showing GUESS A NUMBER 1-9? 7, then CORRECT!, then GAME OVER.
The guess matched, so THEN ran: CORRECT! prints, then GAME OVER. Guess anything else and only GAME OVER appears — the THEN is skipped.

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:

Step 2: one IF per outcome
+2-1
11 10 INPUT "GUESS A NUMBER 1-9";G
22 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"
45

Three tests — equal, less-than, greater-than — cover every case. RUN it and guess 3:

The C64 screen showing GUESS A NUMBER 1-9? 3, then TOO LOW.
Only the matching test fired: 3 is less than 7, so TOO LOW prints and the other two IFs stay silent.

< 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 ERROR after THEN. THEN wants 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 IF that prints a message only when G=7.
  • Words, not numbers. INPUT "YES OR NO";A$ then IF A$="YES" THEN PRINT "GREAT". Strings compare too.
  • Jump instead of print. Change a THEN PRINT… to THEN 100 and add a line 100 — the test now decides where the program goes, the seed of Unit 8.

What you've learnt

  • IF tests a condition; THEN runs the rest of the line only when it's true.
  • = compares after IF (and assigns after a name); <, > and <> are the other comparisons.
  • The C64 has no ELSE — write one IF per 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.