User Tools

Site Tools


k10:k10.3:k10.3.1:start

10.3.1 Count loop - FOR control structure

This chapter describes the FOR control structure. It is a form of loop control structures with a fixed number of repetitions.

10.3.1.1 Syntax

Syntax for the FOR control structure:

FOR Variable = Expression { TO | DOWNTO } Expression [ STEP Expression ] 
   <Instruction(s) >
NEXT

10.3.1.2 Notes on the syntax

  • Repeats a sequence of instructions, increasing (incrementing) or decreasing (decrementing) the value of a (loop) variable.
  • In this form of repetition of an instruction or instruction sequence, both the number of repetitions of the <instruction (s)> and the step size STEP are already known at the start of the counting loop - either fixed or calculated.
  • The loop variable must fulfill two conditions: It must be a local variable and of type: Byte, Short, Integer, Long or Float.
  • If the step size is of type Float, then the (loop) variable must also have this type.
  • If the keyword DOWNTO is used instead of TO, the opposite value of the increment is used.
  • If the value of the start expression is greater than that of the end expression - with positive values for the step size - or if the start expression is smaller than that of the end expression with negative value for the step size, then the FOR control structure is not executed.

10.3.1.3 Example 1

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 

10.3.1.4 Example 2

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

10.3.1.5 Example 3

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

10.3.1.6 Example 4

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

10.3.1.7 Notes on semantics

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

Formel

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.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.3/k10.3.1/start.txt · Last modified: 23.09.2023 by honsek

Page Tools