In most programming languages, there are multiple ways to accomplish the same task and QBASIC is no exception. Last post, I revisited the syntax of the FOR...NEXT loop and showed how you could program model of a countdown timer. Here is another way to achieve the same results.
The correct syntax is
DO
' BASIC code statements here
LOOP WHILE c
where
c = a condition is which must be met
in order to halt repetition
This is the code:
FOR...NEXT loops tend to be used most often when the programmer knows the exact starting and stopping conditions (like a countdown or visiting each element of an array [more on arrays later]). DO...WHILE loops tend to be used more often when the program doesn't know how many times the loop should execute (like when asking if the user wants to continue a game, y/n).
A DO...LOOP, without the WHILE can also be coded. The thing is that this particular loop can easily turn into an infinite loop. An infinite loop is one in which the code does not have a way to stop without the user explicitly breaking the loop.
Here is an example of an infinite loop:
The numbers fly on by quickly and you have to stop it using the Ctrl-Break key combination.
The moral here? Always practice safe repetition.