|
This is one of the most common loops used in programs. It allows to execute a block of instructions between the keyword For and Next a specified number of time.
The syntax is the following:
For counter = Val1 To Val2 Step Inc "Block of instructions to execute" Next counter
"Counter" is a variable which can be used inside the loop itself. "val1" is the starting value which is assumed by Counter, "val2" is the maximum value that can be reach by counter. "Inc" is the incremental step for counter.
Initially "counter" assume the same value as "val1", and every time the loop is executed "counter" is incremented or decremented according with "Inc" until the value "val2" will be reached.
Let's see and example:
For counter = 0 To 10 Step 2
Counter will have value of 0 first, then 2, 4,6,8 and eventually 10.
It is not mandatory to specify the instruction Step and the increment. If they are not specified, the default increment is set to one.
For counter = 0 To 10
Counter will have value of 0 first, and then incremented by a unity, therefore assuming values of 1, 2,3,4 etc, until it reaches the value of 10.
Usually Step is mandatory only to the reverse cycle where counter start from a high value and reaches a lower value, decrementing its value specified by "Inc". The value of "Inc" in this case is negative.
Let's see and example:
For counter = 10 To 0 Step -1
Counter will have value of 10 first, and then decremented by a unity, therefore assuming values of 9, 8,7,6 etc, until it reaches the value of 0.
If the keyword Step is omitted, the default increment is 1, and the interpreter will never execute the loop For assuming that the counter has already reach it final value. Because the keyword Step is optional, the interpreter will not give any error for "reverse loop". This is why a special care should be given when writing reverse loops. To check exactly how a loop will be executed, it is very convenient to use the Editor feature to execute the code line by line.
It is also possible to terminate the For loop before the final condition will be met. This can be particularly useful when seeking a specific value among many others. Once the value has been found, it will be just a waste of time to continue with the rest of the loop.
An early exit can be done using the command Goto Label, where the Label will be positioned after the Next keyword in a For loop.
Let's see and example:
... ... Count = 0 For Index = 0 To 10 If (Index^2 = 9) Then Goto ExitForLoop EndIf = Count + 1 Next Index ExitForLoop: ... ... ...
In this example the variable "Count", keeps track of the number of time the For loops is executed. The exit condition will be reached when Index at the power of 2 is equal to 9, i.e. when Index = 3. When exiting from the loop, Count = 2 because the line where is incremented is executed only twice.
Inside For or Do Until loops, there can be also other nested loops.
... ... For IndexA = 0 To 10 For IndexB = 5 To 20 For IndexC = 0 To 3 Next IndexC Next IndexB Next IndexA ... ...
Special care should be taken to make sure that every loop has a different index variable, and every loop is entirely contained into another.
|