BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
13

Round And Round

Introduction

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.

REPEAT And UNTIL

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.

Several Statements On One Line

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.

GOTO

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.

The Code

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.

A REPEAT loop that waits for the right code

Change One Thing

  • Change line 40 to UNTIL TRUE. Run it and give a wrong code. How many times are you asked, and why?
  • Type the two-line GOTO program above and run it. Stop it with ESC. Which line does BBC BASIC say it was on?
  • Type 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?

Exercises

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.

When It Goes Wrong

SymptomCause
The loop never endsThe test after UNTIL never comes true. ESC stops it.
The loop ran once when it should not haveThe test is at the bottom, so a REPEAT loop always runs once.
The > appears on the end of your outputThe last thing printed ended with ;. A PRINT on its own finishes the line.
Numbers printed with ; still have gaps in frontEach new PRINT pads its first number.

Summary

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.

Next

S14, Lists Of Things - one name for a whole row of values.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.