This chapter describes the FOR control structure. It is a form of loop control structures with a fixed number of repetitions.
Syntax for the FOR control structure:
FOR Variable = Expression { TO | DOWNTO } Expression [ STEP Expression ] <Instruction(s) > NEXT
In these 4 part examples, different types are used for the (loop) variable, different step sizes and the keywords TO and DOWNTO:
Public Sub btnForToNext_Click() Dim iCount As Integer Dim fCount As Float For iCount = 1 To 7 Step 1 ' Step 1 kann entfallen → Standard-Schrittweite Print iCount;; Next Print For fCount = 1 To 7.3 Step 0.8 Print fCount;; Next Print For iCount = 12 To 7.3 Step -1 ' Alternative Notation für DOWNTO Print iCount;; Next Print For fCount = 5.3 DownTo 1 Step 0.5 Print fCount;; Next End
This is displayed in the console of the Gambas IDE:
1 2 3 4 5 6 7 1 1,8 2,6 3,4 4,2 5 5,8 6,6 12 11 10 9 8 7 5,3 4,8 4,3 3,8 3,3 2,8 2,3 1,8 1,3
The contents of a file are read out line by line and stored in an array as long as lines are still readable. The content of the array (test option) is then displayed in the console of the IDE for control:
hFile = Open sRubrikPfad For Input While Not Eof(hFile) Line Input #hFile, sZeile aSuchDateiMatrix.Add(sZeile) Wend Close #hFile aSuchDateiMatrix.Sort(0) ' For control: FOR k = 0 TO aSuchdateiListe.Max PRINT aSuchdateiListe[k] NEXT ' k
Using two nested FOR control structures, the simple sorting algorithm Selectionsort (→ Wikipedia: http://de.wikipedia.org/wiki/Selectionsort) is implemented for an integer array:
Private Sub Selectionsort(aArray As Integer[]) Dim iI, iJ, iMin As Integer For iI = 0 To aArray.Max iMin = iI For iJ = iI To aArray.Max If aArray[iJ] < aArray[iMin] Then iMin = iJ Next Swap aArray[iI], aArray[iMin] Next End
The join() method of the class String[] connects the elements of the array in sequence by a separator string - for example, a comma. This simplifies the output of arrays for testing purposes. Unfortunately, this method is missing in the other native array classes, as in Integer[], although using the Str$ () function the representation of a value of any data type is returned as a string. A FOR control structure can be used to implement such a generic join () function:
Private Function GenericJoin(aArray As Variant[], sSep As String) As String Dim iInd As Integer Dim sRes As String For iInd = 0 To aArray.Max sRes &= Str$(aArray[iInd]) & sSep Next Return Left$(sRes, - Len(sSep)) End
The expressions in the head of the FOR control structure are evaluated once - to enter the loop. Thus, a FOR control structure differs from while or repeat loops, which evaluate the expressions in their conditions before and after each iteration.
Care should therefore be taken when using non-constant expressions, as you will see in the following three examples:
Example 1
Private Sub BrokenInfiniteLoop() Dim i As Integer For i = 0 To i + 1 Print i Next End
At first glance, you would assume that the condition is unattainable: i+1 will always be greater than i. However, the expression i+1 will only be evaluated once at the beginning of the loop, i. e. 0 + 1 = 1, since i is equal to 0 at this point in time. The procedure is actually terminated after the following output in the console:
0 1
Example 2
Private Sub BrokenSqr(n As Integer) Dim i As Integer, j As Integer = 1 For i = 1 To n Step 2 * j - 1 Inc j Next Dec j ' For control: Print "j="; j, "sqr(n)="; Int(Sqr(n)) End
This routine is to determine the integer part of the square root of the integer variable n and store it in j. The idea of this routine lies in identity
However, the step expression is only evaluated at the beginning of the FOR control structure. Since at this point in time j = 1, the step expression is constant 2 * 1 - 1 = 1, followed by the output:
j=20 sqr(n)=4
Example 3
Private Sub BrokenArrayRemove() Dim a As Integer[] = [1, 2, 3, 4] Dim i As Integer ' Remove all odd numbers from the array For i = 0 To a.Max If a[i] Mod 2 Then a.Remove(i) ' Correct index after removal to avoid skipping an element Dec i Endif Next Catch Print "Error: "; Error.Text;; "with i="; i End
Remove all odd numbers from the array. When evaluating the end expression, a.Max = 3 is determined. Therefore, at this point in time, the interpreter already knows that the loop has to be executed exactly 3 - i + 1 = 4 times, since i = 0, but in the first iteration a[i] = a[0] = 1 is recognized as odd and removed. Then a.Max = 1, the loop is executed up to and including i = 3, because this value was determined by the interpreter at the beginning of the loop. The output of the procedure is therefore
Error: Out of bounds with i=2
Solution: For applications where the end or step expression is variable, you should use while or repeat loop control structures.