10.2.2 WHILE...NEXT Loops
A WHILE...NEXT statement uses a conditional expression to
control loop execution; the loop is executed as long as a given
condition is true. A WHILE...NEXT loop is useful when you
do not know how many loop iterations are required.
In the following example, the first statement instructs the
user to input data and then type DONE when finished.
After the user enters the first piece of input, BASIC exe-
cutes the WHILE...NEXT loop. If the first input value is not
``DONE'', the loop executes and prompts the user for an-
other input value. Once the user enters this input value, the
WHILE...NEXT loop once again checks to see if this value
corresponds to ``DONE''. The loop will continue executing
until the user types ``DONE'' in response to the prompt.
INPUT 'Type "DONE" when finished'; Answer
WHILE (Answer <> "DONE")
.
.
.
INPUT "More data"; Answer
NEXT
Note that the NEXT statement in the WHILE...NEXT and
UNTIL...NEXT loops does not increment a control vari-
able; your program must change a variable in the conditional
expression or the loop will execute indefinitely.
The evaluation of the conditional expression determines
whether the loop executes. The test is performed (that is, the
conditional expression is evaluated) before the first iteration;
if the value is false (0), the loop does not execute.
It can be useful to intentionally create an infinite loop by cod-
ing a WHILE...NEXT loop whose conditional expression is
always true. When doing this you must take care to provide
a way out of the loop. You can do this with an EXIT state-
ment or by trapping a run-time error. See Chapter 16 for
more information about trapping run-time errors.