
FOR repeats a set number of times. Often you do not know the number. Keep asking for the code until it is right; keep going until the air runs out. That kind of loop - "until something happens" - is the one a game lives in.
10 REPEAT
20 INPUT "ENTER THE CODE",C$
30 IF C$<>"4712" THEN PRINT "ACCESS DENIED"
40 UNTIL C$="4712"
Everything between REPEAT and UNTIL is done, and then the test after UNTIL is made. If it is false, back to REPEAT and round again. If it is true, the program goes on past UNTIL.
Like FOR, a REPEAT loop always runs at least once, because the test is at the bottom. And LIST indents the lines inside it, as it does for FOR.
A colon lets you put more than one statement on a line:
10 N=0:REPEAT N=N+1:PRINT N;:UNTIL N=5
is a whole loop on one line. Use it for short things that belong together; a long line of colons is hard to read and harder to EDIT.
There is an older way to go round:
10 PRINT "ROUND AND ";
20 GOTO 10
GOTO 10 sends the program back to line 10, for ever. You will see GOTO in old programs, and it works, but a program made of GOTOs is hard to follow - you cannot see where the loops are. This course uses REPEAT and FOR, and later something better still.
That program never ends by itself, so it is a good moment to use ESC: it stops at once, with Escape at line and the line it was on.
10 REPEAT
20 INPUT "ENTER THE CODE",C$
30 IF C$<>"4712" THEN PRINT "ACCESS DENIED"
40 UNTIL C$="4712"
50 PRINT "ACCESS GRANTED"
Starting from
A fresh boot, or NEW.
What you should see
Answering 1234 and then 4712:
>RUN
ENTER THE CODE? 1234
ACCESS DENIED
ENTER THE CODE? 4712
ACCESS GRANTED
>
LIST shows lines 20 and 30 indented inside the loop.

UNTIL TRUE. Run it and give a wrong code. How many times are you asked, and why?GOTO program above and run it. Stop it with ESC. Which line does BBC BASIC say it was on?10 N=0:REPEAT N=N+1:PRINT N;:UNTIL N=5 and run it. Why does each number get its own ten-column space, even with the ;? And where does the > end up?13.1 Keep asking for numbers until 0 is typed, then print the total of all of them.
13.2 Count down from 10 to 1 with REPEAT, not FOR.
13.3 Start with 100 and keep halving it until it is less than 1. How many halvings did it take?
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| The loop never ends | The test after UNTIL never comes true. ESC stops it. |
| The loop ran once when it should not have | The test is at the bottom, so a REPEAT loop always runs once. |
The > appears on the end of your output | The last thing printed ended with ;. A PRINT on its own finishes the line. |
Numbers printed with ; still have gaps in front | Each new PRINT pads its first number. |
REPEAT ... UNTIL test goes round until the test is true, and always at least once. A colon puts statements side by side. GOTO jumps to a line; it works, and it is best kept for emergencies. ESC stops anything.
S14, Lists Of Things - one name for a whole row of values.