Loops

From Wiki
Revision as of 14:32, 1 September 2011 by Sancho (talk | contribs) (Created page with "<section begin=title /><noinclude>{{DISPLAYTITLE:</noinclude>Loops<noinclude>}}</noinclude><section end=title />{{BreadCrumbL10n}} {{Documentation/BASICGuideTOC |ShowPrevNext=bl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search





A loop executes a code block for the number of passes that are specified. You can also have loops with an undefined number of passes.

For...Next

The For...Next loop has a fixed number of passes. The loop counter defines the number of times that the loop is to be executed. In the following example, variable I is the loop counter, with an initial value of 1. The counter is incremented by 1 at the end of each pass. When variable I equals 10, the loop stops.

Dim I 
For I = 1 To 10
  ' ...  Inner part of loop 
Next I

If you want to increment the loop counter by a value other than 1 at the end of each pass, use the Step function:

Dim I 
For I = 1 To 10 Step 0.5
  ' ... Inner part of loop 
Next I

In the preceding example, the counter is increased by 0.5 at the end of each pass and the loop is executed 19 times.

You can also use negative step values:

Dim I 
For I = 10 To 1 Step -1
  ' ... Inner part of loop 
Next I

In this example, the counter begins at 10 and is reduced by 1 at the end of each pass until the counter is 1.

The Exit For instruction allows you to exit a For loop prematurely. In the following example, the loop is terminated during the fifth pass:

Dim I 
For I = 1 To 10 
  If I = 5 Then 
    Exit For
  End If
  ' ... Inner part of loop 
Next I

For Each

The For Each...Next loop variation in VBA is supported in OpenOffice.org Basic. For Each loops do not use an explicit counter like a For...Next loop does. A For Each loop says "do this to everything in this set", rather than "do this n times". For example:

Const d1 = 2
Const d2 = 3
Const d3 = 2
Dim i
Dim a(d1, d2, d3)
For Each i In a()
    ' ... Inner part of loop 
Next i

The loop will execute 36 times.

Do...Loop

The Do...Loop is not linked to a fixed number of passes. Instead, the Do...Loop is executed until a certain condition is met. There are four versions of the Do...Loop. In the first two examples, the code within the loop may not be executed at all ("do 0 times" logic). In the latter examples, the code will be executed at least once. (In the following examples, A > 10 represents any condition):

  1. The Do While...Loop version
    Do While A > 10
       ' ... loop body
    Loop
    checks whether the condition after the While is true before every pass and only then executes the loop.
  2. The Do Until...Loop version
    Do Until A > 10
      ' ... loop body
    Loop
    executes the loop as long as the condition after the Until evaluates to false.
  3. The Do...Loop While version
    Do
      ' ... loop body
    Loop While A > 10
    only checks the condition after the first loop pass and terminates if the condition after the While evaluates to false.
  4. The Do...Loop Until version
    Do
      ' ... loop body
    Loop Until A > 10
    also checks its condition after the first pass, but terminates if the condition after the Until evaluates to true.

As in the For...Next loop, the Do...Loop also provides a terminate command. The Exit Do command can exit at loop at any point within the loop.

Do 
  If A = 4 Then
    Exit Do
  End If
  ' ... loop body
Loop While A > 10

In some cases the loop may only terminate when a condition is met within the loop. Then you can use the "perpetual" Do Loop:

Do 
  ' ... some internal calculations
  If A = 4 Then    Exit Do
  ' ... other instructions
Loop

While...Wend

The While...Wend loop construct works exactly the same as the Do While...Loop, but with the disadvantage that there is no Exit command available. The following two loops produce identical results:

Do While A > 10
   ' ... loop body
Loop

While A > 10
   ' ... loop body
Wend

Programming Example: Sorting With Embedded Loops

There are many ways to use loops, for example, to search lists, return values, or execute complex mathematical tasks. The following example is an algorithm that uses two loops to sort a list by names.

Sub Sort
  Dim Entry(1 To 10) As String
  Dim Count As Integer
  Dim Count2 As Integer
  Dim Temp As String

  Entry(1) = "Patty"
  Entry(2) = "Kurt"
  Entry(3) = "Thomas"
  Entry(4) = "Michael"
  Entry(5) = "David"
  Entry(6) = "Cathy"
  Entry(7) = "Susie"
  Entry(8) = "Edward"
  Entry(9) = "Christine"
  Entry(10) = "Jerry"

  For Count = 1 To 9
    For Count2 = Count + 1 To 10
      If Entry(Count) > Entry(Count2) Then
        Temp = Entry(Count)
        Entry(Count) = Entry(Count2)
        Entry(Count2) = Temp
      End If
    Next Count2
  Next Count

  For Count = 1 To 10
    Print Entry(Count)
  Next Count

End Sub

The values are interchanged as pairs several times until they are finally sorted in ascending order. Like bubbles, the variables gradually migrate to the right position. For this reason, this algorithm is also known as a Bubble Sort.


Content on this page is licensed under the Public Documentation License (PDL).