Imagine that you want to check the tyre pressure for lorries and motor bikes, as well as for four-wheeled cars. (See Sec..) The algorithm to do this is something like:
CHECK IF THERE ARE MORE TYRES LEFT MOVE TO NEW TYRE MEASURE PRESSURE IN TYRE GO BACK TO START
As can be seen, the above program/algorithm keeps going around in a loop so long as a certain condition is satisfied (i.e. so long as there are more tyres to be checked). Basic has a loop in exactly this style.
Consider the following program (note that str_My_Number should be the name of a text box):
My_Number = Val(str_My_Number) i = 0 While i <> My_Number i = i + 1 Wend Print "I think that your number is "; i
In this example, the loop begins with While and ends with Wend. Inside this While loop the variable i is increased by one each time through the loop. The While loop stops when i is equal to My_Number. Note that the symbol <> means "not equal to" (see ). A working version of this programme appears here.
You can see that in this case the For/Next loop is of no use since you don't know in advance how many times you want to go around the loop.
While loops are similar to For/Next loops in that they are used when the same lines of code need to be repeated more than once. However While loops are typically more flexible - the loop stops when some condition is no longer satisfied.