
FOR is for when you know how many times. Often you do not: keep asking until the answer is right, keep the game going until the mouse is caught. That needs a different shape of loop, and it needs something FOR gave you for free - a way for the loop to end.
This section also covers the most important habit in the whole course, which is making sure you can always stop.
GOTO sends the program to a line number:
10 N=1
20 PRINT N;
30 N=N+1
40 IF N<=5 THEN GOTO 20
50 PRINT "DONE"
That is FOR written out longhand - and you would use FOR for it. GOTO earns its place when the thing that ends the loop is not a count.
Two limits worth knowing now. The line number must be a real line: a jump to a line that is not there is a Branch Error. And it must be a number you have written down - GOTO A, with the line number in a variable, is a Branch Error too. This BASIC will not work out where to jump.
Here is a loop with nothing to stop it:
30 PTS=PTS+1
50 GOTO 30
Type that, run it, and the machine is gone. Under the emulator no key will stop a running program - not ESC, not BREAK, nothing - so the only way back is to close the MAME window and start again, losing everything you have not saved.
The cure is to have the loop look at the keyboard itself. KBD from S10 gives you the code of a key that is being pressed, or 0 if none is, and it does not wait:
40 IF KBD<>0 THEN 60
One line, in every loop that has no other way to end. It costs a fraction of the loop's speed and it means a mistake costs you nothing.
Do this from now on, in this course and afterwards. A program you cannot get out of is a program that eats your work.
END stops the program with nothing to say. STOP stops it and says where:
Break in 40
and after a STOP - and only after a STOP - CONT carries on from where it left off, with every variable as it was. That makes STOP a way of looking inside a program that is misbehaving.
GOSUB is GOTO that remembers where it came from:
10 GOSUB 100
20 PRINT "BACK IN THE MAIN PROGRAM"
30 END
100 PRINT "IN THE SUBROUTINE"
110 RETURN
RETURN comes back to the statement after the GOSUB. That gives you a piece of program you can call from several places - drawing the score, moving a cat - without copying it out each time.
Line 30's END matters. Without it the program would run on into line 100, do the subroutine a second time, and hit RETURN with nothing to return to: Return Error.
Subroutines can call subroutines, about thirty deep. Past that you get Stack Full Error, which in practice means something has gone round in circles.
ON picks a destination from a list:
20 ON A GOTO 100,200,300
goes to 100 if A is 1, 200 if it is 2, 300 if it is 3. It is the tidy way to act on a menu choice, and it is as close to GOTO A as this BASIC gets.
10 PTS=0
20 PRINT "COUNTING - HOLD ANY KEY TO STOP"
30 PTS=PTS+1
40 IF KBD<>0 THEN 60
50 GOTO 30
60 PRINT "YOU STOPPED IT AT";PTS
Starting from
A fresh boot, or NEW.
What you should see
The message, then nothing at all while it counts, and when you hold a key down:
COUNTING - HOLD ANY KEY TO STOP
YOU STOPPED IT AT 827
Ready

Your number will not be 827. It depends on how long you took, and it is worth looking at: that is roughly how many times a three-line loop goes round in a couple of seconds, and it is the figure the game in part three has to live within.
PTS is still there afterwards - the loop ended tidily rather than being killed, so you can PRINT PTS and see it.
IF KBD=32 THEN 60, so that only the space bar stops it. Now hold another key. What happens, and why is <>0 usually the safer test?STOP, run it, stop it, and then type CONT. What does the program do, and what does PRINT PTS say?13.1 Ask for a number and keep asking until the answer is between 1 and 10, then print it. (This loop ends because of the answer, so it needs no KBD line - but say to yourself why.)
13.2 Write a program that counts up from 1 for ever, printing every hundredth number, and stops when a key is held. Use MOD from S7 to decide when to print.
13.3 Write a subroutine at line 500 that prints a row of 30 dashes, and call it from three different places in a program.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Branch Error in <line> | The line you jumped to is not there - or you wrote GOTO A, which this BASIC cannot do. Use ON ... GOTO. |
Return Error in <line> | A RETURN was reached without a GOSUB - usually a program running on into its own subroutines. Put END in front of them. |
Stack Full Error | Subroutines calling one another round in a circle, or a GOSUB inside a loop that never returns. |
| The program will not stop and no key helps | There is no KBD line in the loop. Restart the Einstein, and put one in. |
CONT gives Cont Error | CONT only works after a STOP, not after an error. |
| The loop is much slower than you expected | Every statement inside it costs. S31 measures what. |
GOTO jumps to a line number - a real one, written down, not in a variable. GOSUB jumps and RETURN comes back. ON x GOTO picks from a list. END stops, STOP stops and says where, and CONT resumes after a STOP. Any loop without a natural end needs IF KBD<>0 THEN ... in it, because nothing outside the program can stop it.
S14, Lists Of Things - one name holding many numbers, so that a program can keep track of more than a handful of things at once.