Documentation/BASIC Guide/Loops

From Apache OpenOffice Wiki
Jump to: navigation, search


Loops

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


Template:Documentation/Note


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 variants of the Do...Loop (in the following examples, A > 10 represents any condition):

  1. The Do While...Loop variant
    Do While A > 10
    ' ... loop body
    Loop
    checks whether the condition is still satisfied before every pass and only then executes the loop.
  2. The Do Until...Loop variant
    Do Until A > 10
    ' ... loop body
    Loop
    executes the loop until the condition is no longer satisfied.
  3. The Do...Loop While variant
    Do
    ' ... loop body
    Loop While A > 10
    only checks the condition after the first loop pass and terminates if this condition is satisfied.
  4. The Do...Loop Until variant
    Do
    ' ... loop body
    Loop Until A > 10
    also checks its condition after the first pass, but undertakes the loop until the condition is no longer satisfied.

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
While A > 10

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 10
    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).
Personal tools