Imagine a program based on the BBC quiz Mastermind. The program needs to

repeatedly ask questions until the time limit of one minute is reached. Can we

use a FOR...NEXT loop here?

FOR...NEXT loops always end as the result of a count reaching a certain value.

Here we have no idea beforehand exactly how many questions will be answered

in one minute. One person running the program may answer only three

questions, whereas another may answer a dozen.

In this case we must use a different sort of loop, the REPEAT ...UNTIL loop

This is a loop that ends when a condition is satisfied, rather than as a result of a

count. For example, many programs include a procedure that prevents the

program from rushing on until a particular key is pressed:

10 MODE 7

20 PR0Cwait

30 END

40 DEFPR0Cwait

50 PRINT TAB(8,24)"Press C to continue"

60 REPEAT

70 key$=GET$

80 UNTIL key$="C"

90 ENDPROC

Lines 60 to 80 REPEATedly scan the keyboard UNTIL the C is pressed. Press

some other key at line 70. The computer finds that key$ does not satisfy the

condition at line 80, and so it executes the loop again from line 60.

The Mastermind program might look something like this:

10 MODE 7

20 PR0Cquiz

30 END

40 DEFPROCquiz

50 TIME=0

60 answers=0

70 REPEAT

80 first=RND(12)

90 second=RND(12)

100 PRINT'"What is ";first;" times ";second;

110 INPUT response

120 answer=answer+1

130 UNTIL TIME>=600

140 PRINT' "You answered " ;answer ;" questions"

150 ENDPROC

C 40