![]() |
For...Next cycle
As we said in the introduction, cycles are pre determined structures within wich the execution of one or more instructions is repeated many times. As an example, let´s look at this image:
![]() |
We agree that:
*) The For...Next structure is a Block type, alike If...End if
*) The limits of the block are given by the key-words: For and Next
*) Within this block there can be one or more than one instruction.
Let´s concetrate now in understanding the parts that determin the behavior of the For block.
The secret is in the instruction that starts the cycle (For)
First we will remember something about the definition of variable:
Variables are a way to identify a portion of memory where we have allocated a data. So we don´t have to remember the physical memory address where the data was stored, we´ll give it an identifier, plus in BASIC identifiers can come along with a reserved symbol that specifies the data type ($ for chars, % for integers, etc). The data type can also be pre-determined in the variable´s declaration.
Alright, let´s go back to where we were: The parts in the definition of the For cycle
For i = 1 to 10
i : the cycle´s identifier. This kind of variables (identifiers used in For cycles) are known by the name of "accumulator". This variable will be added 1 unit on each round of the cycle, and the result of that addition will be stored in the same variable. In this way, we will know on wich round we are every time that it returns to the For instruction. If one of the instructions inside the cycle changes this variable´s value, there will be a conflict within the cycle, and it may not be able to finish all the rounds. That is why we can use the accumulator to evaluate its content (use it on an if block) but we can´t store data on it. Within the For block the accumulator is a reserved variable. For this reason, it´d be wise to get used to a same identifier to use in For cycles (the most common case is the 'i' letter) and that you use that identifier only for cycling blocks, and not for anything else.
1 to 10 :
The range of the cycle. Obviously the first number is the value of i at the beginning of the cycle, and the second is the value that i must have before exiting the cycle. Inspite of this, if the range goes from 0 to 10 the cycle will make 11 turns (atention!). As a corollary of this part, the values in the range may take the value of any integer number. As examples:
-1 to 1 (3 turns)
-10 to 9 (20 turns)
0 to 1 (2 turns)
Next i : When the For cycle was initiated, execution continues until the following Next that is accompanied by the same identifier that initiated it (i). i.e.: If we start the For block with the identifier my_counter (For my_counter = 1 To 100) and at the end of the block we state Next i, there won´t be a coherence in the cycle and an error we´ll be casted out.
For...Next cycle´s flexibility:
We may affirm that the For cycle is very flexible, provided the posibilties that it offers to configure it throughout run-time (¿?).
Until now, the only identifier we saw interceding in the block, was the accumulator (i). But there can be more. The range can be defined by identifiers that might receive values in other procedures of the program. As an example, let´s take a look at this simple program:
![]() |
By changing the value in Text1.Text a different value will be passed to the variable for_stop and the cycle will have a new finish value. It means that the cycle will be performed in the range 1 --- To ---> for_stop.
Always keep in mind that the value stored in for_stop comes from the TextBox.
In the same way we can replace the whole structure by identiers (variables) of our convenience. Let´s take a look to this For cycle:
For i = for_start To for_stop Text1.Text = Text1.Text & vbCrLf & "i:" & i Next i |
In this cycle, every parameter has been replaced by identifiers. In this way, we have total control over the cycle and we can configure it during execution (run-time). If the variables for_start and for_stop have no value assigned when the cycle commences (when 'comenzar' button is clicked) these variables will have a value of 0 (zero) and there might be problems to execute the cycle.
We will pass now to the last part of the For cycle. The Step parameter.
Well, you know step. It´s pure english.
It´s referred to the interval that will be added to the accumulator to pass to the next round of the cycle.
With this parameter we can have a For cycle that accumulates values 2 by 2, a 100 by a 100, etc.
Let´s check out this example:
For i = 1 To 20 Step 5 Text1.Text = Text1.Text & vbCrLf & "i:" & i Next i |
In this example the For cycle will have the following values:
1
6
11
16
21 (<- end of the For cycle.)
In this way, we produce a cycle that only takes four turns with a range defined between 1 and 20.
The use of Step is very diverse and the programmer decides its best use according to every circumstance.
Ok, I hope that you´ve learnt enough about the For cycle. You´ll see that it will come very ahandy for developing programs that do recursive tasks in an easy and flexible way. If you´ve played enough with it and are well familiarized with it, I invite you to continue to the next phase of this tutorial...
Chapter #2: Do...Loop cycle.