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

From Apache OpenOffice Wiki
Jump to: navigation, search
(For Each)
(Do...Loop)
Line 72: Line 72:
 
== <tt>Do...Loop</tt> ==
 
== <tt>Do...Loop</tt> ==
  
The <tt>Do...Loop</tt> is not linked to a fixed number of passes. Instead, the <tt>Do...Loop</tt> is executed until a certain condition is met. There are four versions of the <tt>Do...Loop</tt>. 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, <tt>A > 10</tt> represents any condition):
+
De <tt>Do...Loop</tt> is niet gekoppeld aan een vast aantal doorlopen. In plaats daarvan wordt de <tt>Do...Loop</tt> uitgevoerd totdat aan een bepaalde voorwaarde wordt voldaan. Er zijn vier varianten van de <tt>Do...Loop</tt>. In de eerste twee voorbeelden zou de code binnen de lus helemaal niet uitgevoerd hoeven worden ("doe 0 keer" logica). In de latere voorbeelden zal de code ten minste één maakl worden uitgevoerd. (In de volgende voorbeelden vertegenwoordigt <tt>A > 10</tt> een willekeurige voorwaarde):
  
 
<ol class="task">
 
<ol class="task">
<li>The <tt>Do While...Loop</tt> version
+
<li>De versie <tt>Do While...Loop</tt>
 
<source lang="oobas">
 
<source lang="oobas">
 
Do While A > 10
 
Do While A > 10
   ' ... loop body
+
   ' ... body van de lus
 
Loop
 
Loop
 
</source>
 
</source>
checks whether the condition after the <tt>While</tt> is '''true''' before every pass and only then executes the loop.</li>
+
controleert of de voorwaarde na de <tt>While</tt> '''true''' is vóór elke doorloop en voert alleen dan de lus uit.</li>
<li>The <tt>Do Until...Loop</tt> version
+
<li>De versie <tt>Do Until...Loop</tt>
 
<source lang="oobas">
 
<source lang="oobas">
 
Do Until A > 10
 
Do Until A > 10
   ' ... loop body
+
   ' ... body van de lus
 
Loop
 
Loop
 
</source>
 
</source>
executes the loop as long as the condition after the <tt>Until</tt> evaluates to '''false'''.</li>
+
voert de lus net zo lang uit totdat de voorwaarde na de <tt>Until</tt> evalueeert naar '''false'''.</li>
<li>The <tt>Do...Loop While</tt> version
+
<li>De versie <tt>Do...Loop While</tt>
 
<source lang="oobas">
 
<source lang="oobas">
 
Do
 
Do
   ' ... loop body
+
   ' ... body van de lus
 
Loop While A > 10
 
Loop While A > 10
 
</source>
 
</source>
only checks the condition after the first loop pass and terminates if the condition after the <tt>While</tt> evaluates to '''false'''.</li>
+
controleert de voorwaarde na de eerste doorloop en eindigt als de voorwaarde na de <tt>While</tt> evalueert naar '''false'''.</li>
<li>The <tt>Do...Loop Until</tt> version
+
<li>De versie <tt>Do...Loop Until</tt>
 
<source lang="oobas">
 
<source lang="oobas">
 
Do
 
Do
   ' ... loop body
+
   ' ... body van de lus
 
Loop Until A > 10
 
Loop Until A > 10
 
</source>
 
</source>
also checks its condition after the first pass, but terminates if the condition after the <tt>Until</tt> evaluates to '''true'''.</li>
+
controleert de voorwaarde na de eerste doorloop, maar eindigt als de voorwaarde na de <tt>Until</tt> evalueert naar '''true'''.</li>
 
</ol>
 
</ol>
  
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.
+
Net zoals in de lus <tt>For...Next</tt> verschaft de <tt>Do...Loop</tt> ook een opdracht voor beëindiging. De opdracht <tt>Exit Do</tt> kan een lus op elk punt in de lus verlaten.
  
 
<source lang="oobas">
 
<source lang="oobas">
Line 112: Line 112:
 
     Exit Do
 
     Exit Do
 
   End If
 
   End If
   ' ... loop body
+
   ' ... body van de lus
 
Loop While A > 10
 
Loop While A > 10
 
</source>
 
</source>
In some cases the loop may only terminate when a condition is met within the loop. Then you can use the "perpetual" Do Loop:
+
In sommige gevallen zou de lus alleen kunnen eindigen wanneer aan een voorwaarde wordt voldaan binnen de lus. Dan kunt u de "perpetuele" Do Loop gebruiken:
  
 
<source lang="oobas">
 
<source lang="oobas">
 
Do  
 
Do  
   ' ... some internal calculations
+
   ' ... enkele interne berekeningen
 
   If A = 4 Then    Exit Do
 
   If A = 4 Then    Exit Do
   ' ... other instructions
+
   ' ... andere instructies
 
Loop
 
Loop
 
</source>
 
</source>

Revision as of 17:24, 21 January 2013

Book.png


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

De Do...Loop is niet gekoppeld aan een vast aantal doorlopen. In plaats daarvan wordt de Do...Loop uitgevoerd totdat aan een bepaalde voorwaarde wordt voldaan. Er zijn vier varianten van de Do...Loop. In de eerste twee voorbeelden zou de code binnen de lus helemaal niet uitgevoerd hoeven worden ("doe 0 keer" logica). In de latere voorbeelden zal de code ten minste één maakl worden uitgevoerd. (In de volgende voorbeelden vertegenwoordigt A > 10 een willekeurige voorwaarde):

  1. De versie Do While...Loop
    Do While A > 10
       ' ... body van de lus
    Loop
    controleert of de voorwaarde na de While true is vóór elke doorloop en voert alleen dan de lus uit.
  2. De versie Do Until...Loop
    Do Until A > 10
      ' ... body van de lus
    Loop
    voert de lus net zo lang uit totdat de voorwaarde na de Until evalueeert naar false.
  3. De versie Do...Loop While
    Do
      ' ... body van de lus
    Loop While A > 10
    controleert de voorwaarde na de eerste doorloop en eindigt als de voorwaarde na de While evalueert naar false.
  4. De versie Do...Loop Until
    Do
      ' ... body van de lus
    Loop Until A > 10
    controleert de voorwaarde na de eerste doorloop, maar eindigt als de voorwaarde na de Until evalueert naar true.

Net zoals in de lus For...Next verschaft de Do...Loop ook een opdracht voor beëindiging. De opdracht Exit Do kan een lus op elk punt in de lus verlaten.

Do 
  If A = 4 Then
    Exit Do
  End If
  ' ... body van de lus
Loop While A > 10

In sommige gevallen zou de lus alleen kunnen eindigen wanneer aan een voorwaarde wordt voldaan binnen de lus. Dan kunt u de "perpetuele" Do Loop gebruiken:

Do 
  ' ... enkele interne berekeningen
  If A = 4 Then    Exit Do
  ' ... andere instructies
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).
Personal tools