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

GO TO, GO SUB, and the Game Loop

Build the loop every game runs on — keep asking until the player wins — with GO TO. Package a job and call it from anywhere with GO SUB and RETURN. And let the computer pick a secret number with RND.

53% of Meet BASIC

A counted loop runs a known number of times. A game runs until something happens — until you win, lose, or quit. That loop is built from GO TO: a jump back to an earlier line, guarded by an IF. Both GO TO and GO SUB are two words on the Spectrum, not one.

Milestone 1 — the game loop

Here's a number-guessing game. We fix the secret to 7 for now, so you can follow the play; in a moment RND will choose it.

  10 LET secret = 7
  20 LET tries = 0
  30 INPUT "Guess (1-10)? "; g
  40 LET tries = tries + 1
  50 IF g < secret THEN PRINT g; " is too low"
  60 IF g > secret THEN PRINT g; " is too high"
  70 IF g <> secret THEN GO TO 30
  80 PRINT "Correct in "; tries; " guesses!"

Line 70 is the loop: IF g <> secret THEN GO TO 30 jumps back to the INPUT whenever the guess is wrong, so the program keeps asking. When the guess is right, line 70 is false, the jump is skipped, and line 80 runs. Played with 4, then 9, then 7:

The Spectrum screen showing 4 is too low, 9 is too high, Correct in 3 guesses!, with the report 0 OK, 80:1.
The game loop: ask, check, jump back if wrong, repeat. This input-check-repeat shape is the heart of every game you'll build, in BASIC and later in assembly.

This is the game loop. You'll meet it in every game — the program keeps running, taking input and responding, until a condition ends it.

Milestone 2 — GO SUB: a job you can call

When you need the same job in several places, give it its own lines and GO SUB to it. RETURN comes back to where you called from:

  10 GO SUB 100
  20 PRINT "Hello"
  30 GO SUB 100
  40 PRINT "Goodbye"
  50 GO SUB 100
  60 STOP
 100 PRINT "============"
 110 RETURN

GO SUB 100 jumps to line 100, runs until RETURN, then comes back to the line after the call. The divider at line 100 is written once and called three times.

The Spectrum screen showing a row of equals signs, Hello, equals signs, Goodbye, equals signs, with the report 9 STOP statement, 60:1.
One divider subroutine, called three times. The report '9 STOP statement' is the machine confirming STOP halted it on purpose — STOP keeps the program from wandering into the subroutine below.

Notice you jump to a line numberGO SUB 100, not a name. Sinclair BASIC has no named subroutines; the number is the name. That's why we leave numbering gaps, and why subroutines usually live at high line numbers, out of the main program's way. The STOP at line 60 matters: without it, the program would run past line 50 straight into the subroutine, hit RETURN with nowhere to return to, and fail.

Milestone 3 — RND picks a number

To make the secret a real surprise, the computer needs an unpredictable number. RND gives a fresh fraction between 0 and 1 each time; INT (RND * 10) + 1 turns it into a whole number from 1 to 10:

  10 FOR i = 1 TO 6
  20 PRINT INT (RND * 10) + 1; " ";
  30 NEXT i
The Spectrum screen showing six numbers between 1 and 10, with the report 0 OK, 30:1.
Six rolls of RND, each from 1 to 10. INT (RND * n) + 1 is the idiom for 'a whole number from 1 to n' — run it again and the numbers change.

Now you can drop it into the game. Replace line 10's LET secret = 7 with LET secret = INT (RND * 10) + 1, and the computer chooses a different secret every time — a real game. (Each run starts the sequence fresh; making it repeat a chosen sequence is a job for later.)

When it doesn't work

  • RETURN without GO SUB. The program reached a RETURN without a matching GO SUB — usually it ran into the subroutine from above. Add a STOP before it.
  • The loop never ends. The IF guarding the GO TO is always true, so it always jumps back. Check the condition that's meant to let it through.
  • GOTO gave Nonsense in BASIC. It's two wordsGO TO, GO SUB.

Before and after

You started able to make single decisions and finished building the game loop — the input-check-repeat shape every game runs on — packaging a job with GO SUB and RETURN, and giving the computer a RND secret. The idea underneath: GO TO jumps to a line to loop; GO SUB calls a numbered subroutine and RETURN comes back; RND makes a value unpredictable.

Try this

  • Make it random. Swap line 10 for LET secret = INT (RND * 10) + 1 and play. You won't know the answer in advance.
  • A wider range. Change 10 to 100 in both the secret and the prompt. How many guesses does it take?
  • Reuse the divider. Add a GO SUB to the divider before and after the game's result.

What you've learnt

  • A GO TO back to an earlier line, guarded by an IF, is the game loop.
  • GO SUB n calls the subroutine at line n; RETURN comes back. Both are two words.
  • You call a subroutine by line number — Sinclair BASIC has no named ones.
  • RND gives a random fraction; INT (RND * n) + 1 is a whole number from 1 to n.

What's next

You now have the whole language: output, variables, input, decisions, loops, and subroutines. Time to make the Spectrum show off. In Unit 9 we leave plain text behind and place it anywhere on screen with PRINT AT — and the title card finds its spot.