Lussen
Een lus voert het codeblok uit over het aantal doorlopen dat werd gespecificeerd. U kunt ook lussen hebben met een niet gedefinieerd aantal doorlopen.
For...Next
De lus For...Next heeft een vast aantal doorlopen. De teller voor de lus definieert het aantal malen dat de lus moet worden uitgevoerd. In het volgende voorbeeld, is variabele I de teller voor de lus, met een oorspronkelijke waarde van 1. De teller wordt verhoogd met 1 bij elk einde van een doorloop. Als variabele I gelijk is aan 10, stopt de lus.
Dim I For I = 1 To 10 ' ... Inner part of loop Next I
Als u de teller voor de lus wilt verhogen met een andere waarde dan 1 aan het einde van elke doorloop, gebruik dan de functie Step:
Dim I For I = 1 To 10 Step 0.5 ' ... Inner part of loop Next I
In het voorgaande voorbeeld, wordt de teller verhoogd met 0.5 aan het einde van elke doorloop en de lus wordt 19 keer uitgevoerd.
U mag ook negatieve waarden voor Step gebruiken:
Dim I For I = 10 To 1 Step -1 ' ... binnenste gedeelte van de lus Next I
In dit voorbeeld begint de teller met 10 en wordt met 1 verlaagd aan het einde van elke doorloop totdat de teller op 1 staat.
De uitdrukking Exit For stelt u in staat een lus For voortijdig te verlaten. In het volgende voorbeeld, wordt de lus beëindigd gedurende de vijfde doorloop:
Dim I For I = 1 To 10 If I = 5 Then Exit For End If ' ... binnenste gedeelte van de lus Next I
For Each
De variatie lus For Each...Next in VBA wordt ondersteund in Apache OpenOffice Basic. Lussen For Each gebruiken geen expliciete teller zoals een lus For...Next doet. Een lus For Each zegt "doe dit voor alles in deze verzameling", in plaats van "doe dit n keer". Bijvoorbeeld:
Const d1 = 2 Const d2 = 3 Const d3 = 2 Dim i Dim a(d1, d2, d3) For Each i In a() ' ... binnenste gedeelte van de lus Next i
De lus zal 36 keer worden uitgevoerd.
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):
- The Do While...Loop version
checks whether the condition after the While is true before every pass and only then executes the loop.
Do While A > 10 ' ... loop body Loop
- The Do Until...Loop version
executes the loop as long as the condition after the Until evaluates to false.
Do Until A > 10 ' ... loop body Loop
- The Do...Loop While version
only checks the condition after the first loop pass and terminates if the condition after the While evaluates to false.
Do ' ... loop body Loop While A > 10
- The Do...Loop Until version
also checks its condition after the first pass, but terminates if the condition after the Until evaluates to true.
Do ' ... loop body Loop Until A > 10
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). |