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.
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:
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:
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:
When it doesn't work
- The program never stops. A
GOTOwith no exit. Give the loop anIF … THEN END(or aTHENthat jumps past it), and remember RUN/STOP to break a runaway. ?RETURN WITHOUT GOSUB ERROR. Control reached aRETURNwithout aGOSUB— usually a routine the program fell into from above. Put anENDbefore your subroutines.- The secret is the same every run. That's
RND(1)on a fresh boot. UseRND(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+1after theINPUTand printTwhen the player wins. - A fresh secret each time. Change
RND(1)toRND(0)and run twice — different secrets. - Reuse a banner. Put your title-card lines in a
GOSUBroutine and call it at the start of the game.
What you've learnt
GOTOjumps to a line; pointed backwards it loops — the shape of the game loop.- RUN/STOP breaks a runaway loop.
GOSUBcalls a routine andRETURNcomes 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.