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

IF / THEN, the Spectrum Way

Make the program decide. IF ... THEN acts only when a condition holds — and Sinclair BASIC has operators of its own: <> for not-equal, AND and OR that are logical not bitwise, and no ELSE, so you write a second IF.

40% of Meet BASIC

A decision lets a program take one path or another. You met the idea in General Programming; in Sinclair BASIC it's IF ... THEN, and there are a few local rules worth knowing before they catch you.

Milestone 1 — a condition that holds

  10 LET secret = 7
  20 LET g = 7
  30 IF g = secret THEN PRINT "Correct!"

IF g = secret THEN PRINT "Correct!" checks whether g equals secret. It does — both are 7 — so the THEN runs. The = here asks a question ("are these equal?"), unlike the = in LET, which stores.

The Spectrum screen showing Correct!, with the report 0 OK, 30:2.
The condition held, so THEN ran. Change either number so they differ, and nothing prints — the line is skipped.

Milestone 2 — three ways, and no ELSE

Sinclair BASIC has no ELSE. To handle more than one outcome, you write more than one IF. Set the guess lower than the secret and test all three cases:

Step 2: three separate IFs — equal, less, greater
+3-1
11 10 LET secret = 7
2- 20 LET g = 7
2+ 20 LET g = 4
33 30 IF g = secret THEN PRINT "Correct!"
4+ 40 IF g < secret THEN PRINT "Too low"
5+ 50 IF g > secret THEN PRINT "Too high"
46

With g of 4 and a secret of 7, only the < line is true, so Too low prints. The operator for "less than" is <, "greater than" is >. (The one to remember: not-equal is <>, not !=.) Each IF is tested in turn; the ones that don't hold are skipped.

The Spectrum screen showing Too low, with the report 0 OK, 50:1.
Three IFs, one true. With no ELSE to lean on, each outcome is its own IF — exactly the shape the guess game needs.

Milestone 3 — combine conditions with AND and OR

AND and OR join conditions. In Sinclair BASIC they are logical — they ask true-or-false questions about whole conditions, not about the bits inside a number:

  10 LET n = 15
  20 IF n >= 1 AND n <= 10 THEN PRINT n; " is in range"
  30 IF n < 1 OR n > 10 THEN PRINT n; " is out of range"

n >= 1 AND n <= 10 is true only when both halves hold; n < 1 OR n > 10 is true when either does. With n of 15, the OR line wins: 15 is out of range. (>= and <= are "at least" and "at most".)

The Spectrum screen showing 15 is out of range, with the report 0 OK, 30:2.
AND needs both halves true; OR needs either. 15 fails the 1-to-10 test, so the OR line prints. This is how a program checks an answer is sensible before using it.

When it doesn't work

  • != gave Nonsense in BASIC. Sinclair BASIC writes not-equal as <>.
  • Two outcomes both printed. You wanted one or the other but wrote conditions that can both be true. With no ELSE, make sure your IFs don't overlap.
  • THEN did nothing. Whatever follows THEN is the action — a PRINT, a LET, a GO TO. A bare condition with nothing after THEN has nothing to do.

Before and after

You started able to store and read values and finished making the program choose: act on a condition, handle three outcomes with three IFs, and combine tests with AND and OR. The idea underneath: IF ... THEN runs its action only when the condition holds; <> is not-equal; there is no ELSE, so more outcomes mean more IFs.

Try this

  • Flip the result. Change the guess to 9 and predict which line prints before you run it.
  • A both-ends check. Print "just right" only when a number is >= 5 AND <= 7.
  • Equal or over. Use >= to print "you passed" when a score is at least 50.

What you've learnt

  • IF condition THEN action runs the action only when the condition holds.
  • Operators: =, <, >, <=, >=, and <> for not-equal.
  • AND / OR are logical — they combine whole conditions.
  • There is no ELSE — write a separate IF for each outcome.

What's next

Your programs make single decisions now. In Unit 7 they start to repeat: FOR ... NEXT runs a block a set number of times, counting as it goes — without you copying a line over and over.