|
Loops
"For To ..Next" Loops
When something must be repeated, use a loop. For Next is a classic one. It allows you to initialise a variable and to increment it of a step that you can define at each loop. You exit from the loop when the variable reach the limit you defined.
For example, to write down the numbers from 3 to
10, we can write this :
Local i as Integer
For i=3 To 10
Locate 3,10*(i-2),"Number : " & i,4
Next
|
Between For and Next, the variable i takes all the values between 3 and 10.
As you can see, the loop's variable must be declared before.
If you want to display all the numbers from 3 to 20 with a step of 3 :
Local i as Integer
For i=3 To 20 Step 3
Clear
Locate 3,10,"Number : " & i,4
WaitKey
Next
|
So you must use the Step
keyword to define incrementation of the loop's
variable.
"Do ..Loop" Loops
When the exact number of iterations is unknown,
you can use the Do Loop instruction.
Clear
Do While a<=10
Locate 1,1,"Hello "&a,4
a = a+1
b = 0
Do
b = b+1
Locate 2,10,b,4
Loop While b<300
Loop
WaitKey |
In this case, the exit of the loop is combinated
with a condition that you define. The instructions
between Do and Loop
are repeated until the condition after While
is false.
While can be at the
beginning or at the end of the loop. Its place sets
at what time the test of the condition will be done.
If it is after Loop
keyword, the loop will be read at least one time,
and the While condition
wil be evaluated at each end of loop.
|