Loops

From Apache OpenOffice Wiki
Jump to: navigation, 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.

DimI
ForI=1To10
' ... Inner part of loop 
NextI

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

DimI
ForI=1To10Step0.5
' ... Inner part of loop 
NextI

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:

DimI
ForI=10To1Step-1
' ... Inner part of loop 
NextI

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:

DimI
ForI=1To10
IfI=5Then
ExitFor
EndIf
' ... Inner part of loop 
NextI

For Each

The For Each...Next loop variation in VBA is supported in Apache OpenOffice 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:

Constd1=2
Constd2=3
Constd3=2
Dimi
Dima(d1,d2,d3)
ForEachiIna()
' ... Inner part of loop 
Nexti

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
    DoWhileA>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
    DoUntilA>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
    LoopWhileA>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
    LoopUntilA>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
IfA=4Then
ExitDo
EndIf
' ... loop body
LoopWhileA>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
IfA=4ThenExitDo
' ... 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:

DoWhileA>10
' ... loop body
Loop
WhileA>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.

SubSort
DimEntry(1To10)AsString
DimCountAsInteger
DimCount2AsInteger
DimTempAsString
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"
ForCount=1To9
ForCount2=Count+1To10
IfEntry(Count)>Entry(Count2)Then
Temp=Entry(Count)
Entry(Count)=Entry(Count2)
Entry(Count2)=Temp
EndIf
NextCount2
NextCount
ForCount=1To10
PrintEntry(Count)
NextCount
EndSub

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



AltStyle によって変換されたページ (->オリジナル) /