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

GOTO, GOSUB, and the Game Loop

Jump and repeat. GOTO sends the program to a line; the endless loop is its game-shaped form. GOSUB calls a named job and RETURN comes back. RND picks a secret, and the guessing loop ties it all together.

53% of Meet C64 BASIC

FOR repeats a fixed number of times. A game repeats until something happens — and to build that you need two more tools: GOTO, which jumps to a line, and GOSUB, which calls a job and comes back. Add RND to pick a secret, and you have a whole little game.

Milestone 1 — GOTO, and the endless loop

GOTO sends the program straight to a line number. Point it back at an earlier line and it loops forever:

10 PRINT "C64 ";
20 GOTO 10

RUN it and the screen fills — line 20 jumps to line 10 again and again, with no way out:

The C64 screen filled completely with C64 C64 C64 repeating.
GOTO 10 loops forever — the screen fills with C64. Press RUN/STOP to break out of a runaway loop.

When a loop runs away like this, RUN/STOP halts it — the C64's emergency brake, worth knowing before you write your first real loop. An endless loop sounds like a bug, but controlled endlessness — repeat until the player wins — is the shape of every game.

Milestone 2 — RND, and the guessing loop

RND(1) gives a fraction between 0 and 1; scaled and INT-ed, it becomes a whole number in a range. Here it picks a secret from 1 to 9, and a GOTO loops the guess until you hit it:

10 S=INT(RND(1)*9)+1
20 INPUT "GUESS 1-9";G
30 IF G=S THEN PRINT "GOT IT!":END
40 IF G<S THEN PRINT "TOO LOW"
50 IF G>S THEN PRINT "TOO HIGH"
60 GOTO 20

Line 10 sets S to INT(RND(1)*9)+1 — a number 1 to 9. Lines 30–50 give one verdict each (no ELSE, remember — one IF per outcome). Line 60 jumps back to the question, and line 30 ends the program when you're right. RUN it and play:

The C64 screen showing GUESS 1-9? 5 TOO HIGH, 3 TOO HIGH, 2 GOT IT!
The game loop: each wrong guess prints a verdict and GOTO sends it round again; a right guess ends it. Here 5 and 3 are too high, 2 is the secret.

That round trip — ask, test, react, loop — is the game loop. Nearly every game you build is a version of it. (RND(1) gives the same sequence on a fresh boot, handy while testing; RND(0) reseeds from the C64's clock for a different secret each run.)

Milestone 3 — GOSUB calls a job, RETURN comes back

GOTO jumps and stays. GOSUB jumps to a line, runs until RETURN, then comes back to just after the call — so you can write a job once and use it from several places:

10 GOSUB 100
20 PRINT "MENU"
30 GOSUB 100
40 END
100 PRINT "=========="
110 RETURN

Lines 10 and 30 both GOSUB 100; the routine prints a divider and RETURNs each time:

The C64 screen showing a row of equals signs, MENU, and another row of equals signs.
One routine at line 100, called twice — RETURN brings control back after each GOSUB. Write the job once, use it anywhere.

When it doesn't work

  • The program never stops. A GOTO with no exit. Give the loop an IF … THEN END (or a THEN that jumps past it), and remember RUN/STOP to break a runaway.
  • ?RETURN WITHOUT GOSUB ERROR. Control reached a RETURN without a GOSUB — usually a routine the program fell into from above. Put an END before your subroutines.
  • The secret is the same every run. That's RND(1) on a fresh boot. Use RND(0) for a clock-seeded number when you want variety.

Before and after

You started able to branch and count, and finished with the two jumps that structure a program: GOTO to loop, GOSUB/RETURN to call a job — and a real guessing game built from them. The idea underneath: GOTO repeats, GOSUB reuses, and the ask-test-react loop is the heart of every game.

Try this

  • Count the guesses. Add T=T+1 after the INPUT and print T when the player wins.
  • A fresh secret each time. Change RND(1) to RND(0) and run twice — different secrets.
  • Reuse a banner. Put your title-card lines in a GOSUB routine and call it at the start of the game.

What you've learnt

  • GOTO jumps to a line; pointed backwards it loops — the shape of the game loop.
  • RUN/STOP breaks a runaway loop.
  • GOSUB calls a routine and RETURN comes back after the call — write a job once, reuse it.
  • RND(1) makes a random number (same on a fresh boot); RND(0) reseeds for variety.

What's next

That closes the language itself — you can write, edit, talk, listen, choose, repeat and loop. Now we meet the machine. In Unit 9 the screen turns out to be plain memory: you'll put a character anywhere on it by POKE-ing a number into the right address.