Contents / Structure within BASIC / Previous chapter / Next chapter / Index


14 REPEAT, UNTIL, TRUE, FALSE

Computers are fundamentally pretty stupid things but their power comes from their ability to repeat things many times - sometimes many millions of times in one second. In this version of BASIC two types of repeating loops can be used. They are called REPEAT...UNTIL and FOR...NEXT loops. This section explains REPEAT...UNTIL loops and the next chapter deals with FOR...NEXT loops.

Do you remember the story about a man starting with one grain of rice and doubling it each time he won a bet? How many times would he have to double his grains of rice to own more than a million grains? In the following program C is a counter showing how many times the number of grains has doubled and X represents the number of grains of rice.

10 X=1

20 C=0

30 REPEAT

40 X=X*2

50 C=C+1

60 UNTIL X>1000000

70 PRINT C,X

>RUN

20 1048576

Lines 30 to 60 are called a REPEAT...UNTIL loop and everything within the loop is repeated until X is greater than one million.

The "terminating condition" in this program is that X is greater than 1000000.

The next program terminates after 15 seconds. Line 40 reads the starting time and the program repeats until the present time minus the starting time is greater than 1500 hundredth of a second - the internal clock ticks a hundred times a second.

10 PRINT "SEE HOW MANY SUMS YOU"

20 PRINT "CAN DO IN 15 SECONDS"

30 PRINT

40 STARTTIME=TIME

50 REPEAT

60 F=RND(12)

70 G=RND(12)

80 PRINT "WHAT IS ";F;" TIMES "G;

90 INPUT H

100 IF H=F*G THEN PRINT "CORRECT" ELSE PRINT "WRONG"

110 PRINT

120 UNTIL TIME-STARTTIME>1500

130 PRINT "TIME UP"

RUN

SEE HOW MANY SUMS YOU

CAN DO IN 15 SECONDS

WHAT IS 6 TIMES 9?72

WRONG

WHAT IS 1 TIMES 4?4

CORRECT

WHAT IS 9 TIMES 8?72

CORRECT

TIME UP

REPEAT. . .UNTIL loops are very useful and should be used frequently. The next program selects random letters (line 20) and times how long it takes you to find and press the appropriate key. It uses two REPEAT . . .UNTIL loops. One of them is used to wait for a particular key to be pressed on the keyboard.

10 REPEAT

20 Z=RND(26)+64

30 PRINT

40 PRINT "PRESS THE KEY MARKED ";CHR$(Z)

50 T=TIME

60 REPEAT UNTIL GET=2

70 PRINT "THAT TOOK YOU"(TIME-T)/100" SECONDS"

80 UNTIL Z=0

>RUN

PRESS THE KEY MARKED Y

THAT TOOK YOU 1.1 SECONDS

PRESS THE KEY MARKED G

THAT TOOK YOU 1.03 SECONDS

Lines 10 and 80 are the main loop and line 60 is a single line REPEAT...UNTIL loop.

Look at line 80. This will stop the REPEAT...UNTIL loop if Z=0. However Z is calculated in line 20 and will have a value between 65 and 90. It will never equal zero, so the program will never stop of its own accord - you have to press the ESCAPE key.

Line 80 says

80 UNTIL Z=0

Z = 0 will never be "true". Z = 0 will always be "false", so line 80 can be replaced with

80 UNTIL FALSE

which just means "go on for ever". This is a far better way of doing things than using Z=0 because you might decide to change Z next time you looked at the program. It is also better to use REPEAT...UNTIL loops in this way than to put at line 80

80 GOTO 20

Using REPEAT...UNTIL keeps this section of the program neatly together. See page 117 for a comment on GOTO.

If you delete line 10, then the computer will meet an UNTIL statement at line 80 with no idea of where the loop is meant to start

>RUN

PRESS THE KEY MARKED A

THAT TOOK YOU 2.09 SECONDS

No REPEAT at line 80

In summary REPEAT...UNTIL should be used for loops which must terminate on some specific condition.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001