Difference between revisions of "Documentation/BASIC Guide/Loops"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 14: Line 14:
 
The <tt>For...Next</tt> 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 <tt>I</tt> 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.
 
The <tt>For...Next</tt> 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 <tt>I</tt> 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  
+
<source lang="oobas">
For I = 1 To 10
+
Dim I  
  ' ...  Inner part of loop  
+
For I = 1 To 10
Next I
+
  ' ...  Inner part of loop  
 +
Next I
 +
</source>
  
 
If you want to increment the loop counter by a value other than 1 at the end of each pass, use the <tt>Step</tt> function:
 
If you want to increment the loop counter by a value other than 1 at the end of each pass, use the <tt>Step</tt> function:
  
Dim I  
+
<source lang="oobas">
For I = 1 To 10 Step 0.5
+
Dim I  
  ' ... Inner part of loop  
+
For I = 1 To 10 Step 0.5
Next I
+
  ' ... Inner part of loop  
 +
Next I
 +
</source>
  
 
In the preceding example, the counter is increased by 0.5 at the end of each pass and the loop is executed 19 times.
 
In the preceding example, the counter is increased by 0.5 at the end of each pass and the loop is executed 19 times.
Line 30: Line 34:
 
You can also use negative step values:
 
You can also use negative step values:
  
Dim I  
+
<source lang="oobas">
For I = 10 To 1 Step -1
+
Dim I  
  ' ... Inner part of loop  
+
For I = 10 To 1 Step -1
Next I
+
  ' ... Inner part of loop  
 +
Next I
 +
</source>
  
 
In this example, the counter begins at 10 and is reduced by 1 at the end of each pass until the counter is 1.
 
In this example, the counter begins at 10 and is reduced by 1 at the end of each pass until the counter is 1.
Line 39: Line 45:
 
The <tt>Exit For</tt> instruction allows you to exit a <tt>For</tt> loop prematurely. In the following example, the loop is terminated during the fifth pass:
 
The <tt>Exit For</tt> instruction allows you to exit a <tt>For</tt> loop prematurely. In the following example, the loop is terminated during the fifth pass:
  
Dim I  
+
<source lang="oobas">
For I = 1 To 10  
+
Dim I  
  If I = 5 Then  
+
For I = 1 To 10  
    Exit For
+
  If I = 5 Then  
  End If
+
    Exit For
  ' ... Inner part of loop  
+
  End If
Next I
+
  ' ... Inner part of loop  
 +
Next I
 +
</source>
  
 
{{Documentation/Note|The <tt>For Each...Next</tt> loop variant in VBA is not supported in {{OOo}} Basic.}}
 
{{Documentation/Note|The <tt>For Each...Next</tt> loop variant in VBA is not supported in {{OOo}} Basic.}}
Line 60: Line 68:
 
As in the <tt>For...Next</tt> loop, the <tt>Do...Loop</tt> also provides a terminate command. The <tt>Exit Do</tt> command can exit at loop at any point within the loop.
 
As in the <tt>For...Next</tt> loop, the <tt>Do...Loop</tt> also provides a terminate command. The <tt>Exit Do</tt> command can exit at loop at any point within the loop.
  
Do  
+
<source lang="oobas">
  If A = 4 Then
+
Do  
    Exit Do
+
  If A = 4 Then
  End If
+
    Exit Do
  ' ... loop body
+
  End If
Loop While A > 10
+
  ' ... loop body
 +
Loop While A > 10
 +
</source>
  
 
== Programming Example: Sorting With Embedded Loops ==
 
== Programming Example: Sorting With Embedded Loops ==
Line 71: Line 81:
 
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.
 
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
+
<source lang="oobas">
  Dim Entry(1 To 10) As String
+
Sub Sort
  Dim Count As Integer
+
  Dim Entry(1 To 10) As String
  Dim Count2 As Integer
+
  Dim Count As Integer
  Dim Temp As String
+
  Dim Count2 As Integer
+
  Dim Temp As String
  Entry(1) = "Patty"
+
 
  Entry(2) = "Kurt"
+
  Entry(1) = "Patty"
  Entry(3) = "Thomas"
+
  Entry(2) = "Kurt"
  Entry(4) = "Michael"
+
  Entry(3) = "Thomas"
  Entry(5) = "David"
+
  Entry(4) = "Michael"
  Entry(6) = "Cathy"
+
  Entry(5) = "David"
  Entry(7) = "Susie"
+
  Entry(6) = "Cathy"
  Entry(8) = "Edward"
+
  Entry(7) = "Susie"
  Entry(9) = "Christine"
+
  Entry(8) = "Edward"
  Entry(10) = "Jerry"
+
  Entry(9) = "Christine"
+
  Entry(10) = "Jerry"
  For Count = 1 To 10
+
 
    For Count2 = Count + 1 To 10
+
  For Count = 1 To 10
      If Entry(Count) > Entry(Count2) Then
+
    For Count2 = Count + 1 To 10
        Temp = Entry(Count)
+
      If Entry(Count) > Entry(Count2) Then
        Entry(Count) = Entry(Count2)
+
        Temp = Entry(Count)
        Entry(Count2) = Temp
+
        Entry(Count) = Entry(Count2)
      End If
+
        Entry(Count2) = Temp
    Next Count2
+
      End If
  Next Count
+
    Next Count2
+
  Next Count
  For Count = 1 To 10
+
 
    Print Entry(Count)
+
  For Count = 1 To 10
  Next Count
+
    Print Entry(Count)
+
  Next Count
End Sub
+
 
 +
End Sub
 +
</source>
  
 
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 [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort].
 
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 [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort].
  
 
{{PDL1}}
 
{{PDL1}}

Revision as of 13:09, 2 April 2008


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 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

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