Difference between revisions of "Documentation/BASIC Guide/Loops"
m |
|||
(13 intermediate revisions by 5 users not shown) | |||
Line 7: | Line 7: | ||
}} | }} | ||
{{DISPLAYTITLE:Loops}} | {{DISPLAYTITLE:Loops}} | ||
− | + | __NOTOC__ | |
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. | 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. | ||
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. | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Dim I | Dim I | ||
For I = 1 To 10 | For I = 1 To 10 | ||
' ... Inner part of loop | ' ... Inner part of loop | ||
Next I | Next I | ||
− | </ | + | </syntaxhighlight> |
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: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Dim I | Dim I | ||
For I = 1 To 10 Step 0.5 | For I = 1 To 10 Step 0.5 | ||
' ... Inner part of loop | ' ... Inner part of loop | ||
Next I | Next I | ||
− | </ | + | </syntaxhighlight> |
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 34: | Line 34: | ||
You can also use negative step values: | You can also use negative step values: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Dim I | Dim I | ||
For I = 10 To 1 Step -1 | For I = 10 To 1 Step -1 | ||
' ... Inner part of loop | ' ... Inner part of loop | ||
Next I | Next I | ||
− | </ | + | </syntaxhighlight> |
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 45: | 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: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Dim I | Dim I | ||
For I = 1 To 10 | For I = 1 To 10 | ||
Line 53: | Line 53: | ||
' ... Inner part of loop | ' ... Inner part of loop | ||
Next I | Next I | ||
− | </ | + | </syntaxhighlight> |
== <tt>For Each</tt> == | == <tt>For Each</tt> == | ||
− | The <tt>For Each...Next</tt> loop | + | The <tt>For Each...Next</tt> loop variation in VBA is supported in {{AOo}} Basic. <tt>For Each</tt> loops do not use an explicit counter like a <tt>For...Next</tt> loop does. A <tt>For Each</tt> loop says "do this to everything in this set", rather than "do this n times". For example: |
− | < | + | <syntaxhighlight lang="oobas"> |
Const d1 = 2 | Const d1 = 2 | ||
Const d2 = 3 | Const d2 = 3 | ||
Const d3 = 2 | Const d3 = 2 | ||
+ | Dim i | ||
Dim a(d1, d2, d3) | Dim a(d1, d2, d3) | ||
For Each i In a() | For Each i In a() | ||
' ... Inner part of loop | ' ... Inner part of loop | ||
Next i | Next i | ||
− | </ | + | </syntaxhighlight> |
+ | The loop will execute 36 times. | ||
== <tt>Do...Loop</tt> == | == <tt>Do...Loop</tt> == | ||
Line 74: | Line 76: | ||
<ol class="task"> | <ol class="task"> | ||
<li>The <tt>Do While...Loop</tt> version | <li>The <tt>Do While...Loop</tt> version | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Do While A > 10 | Do While A > 10 | ||
' ... loop body | ' ... loop body | ||
Loop | Loop | ||
− | </ | + | </syntaxhighlight> |
checks whether the condition after the <tt>While</tt> is '''true''' before every pass and only then executes the loop.</li> | checks whether the condition after the <tt>While</tt> is '''true''' before every pass and only then executes the loop.</li> | ||
<li>The <tt>Do Until...Loop</tt> version | <li>The <tt>Do Until...Loop</tt> version | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Do Until A > 10 | Do Until A > 10 | ||
' ... loop body | ' ... loop body | ||
Loop | Loop | ||
− | </ | + | </syntaxhighlight> |
executes the loop as long as the condition after the <tt>Until</tt> evaluates to '''false'''.</li> | executes the loop as long as the condition after the <tt>Until</tt> evaluates to '''false'''.</li> | ||
<li>The <tt>Do...Loop While</tt> version | <li>The <tt>Do...Loop While</tt> version | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Do | Do | ||
' ... loop body | ' ... loop body | ||
Loop While A > 10 | Loop While A > 10 | ||
− | </ | + | </syntaxhighlight> |
only checks the condition after the first loop pass and terminates if the condition after the <tt>While</tt> evaluates to '''false'''.</li> | only checks the condition after the first loop pass and terminates if the condition after the <tt>While</tt> evaluates to '''false'''.</li> | ||
<li>The <tt>Do...Loop Until</tt> version | <li>The <tt>Do...Loop Until</tt> version | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Do | Do | ||
' ... loop body | ' ... loop body | ||
Loop Until A > 10 | Loop Until A > 10 | ||
− | </ | + | </syntaxhighlight> |
also checks its condition after the first pass, but terminates if the condition after the <tt>Until</tt> evaluates to '''true'''.</li> | also checks its condition after the first pass, but terminates if the condition after the <tt>Until</tt> evaluates to '''true'''.</li> | ||
</ol> | </ol> | ||
Line 105: | Line 107: | ||
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. | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Do | Do | ||
If A = 4 Then | If A = 4 Then | ||
Line 112: | Line 114: | ||
' ... loop body | ' ... loop body | ||
Loop While A > 10 | Loop While A > 10 | ||
− | </ | + | </syntaxhighlight> |
+ | In some cases the loop may only terminate when a condition is met within the loop. Then you can use the "perpetual" Do Loop: | ||
+ | |||
+ | <syntaxhighlight lang="oobas"> | ||
+ | Do | ||
+ | ' ... some internal calculations | ||
+ | If A = 4 Then Exit Do | ||
+ | ' ... other instructions | ||
+ | Loop | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == <tt>While...Wend</tt> == | ||
+ | |||
+ | The <tt>While...Wend</tt> loop construct works exactly the same as the <tt>Do While...Loop</tt>, but with the disadvantage that there is no <tt>Exit</tt> command available. The following two loops produce identical results: | ||
+ | |||
+ | <syntaxhighlight lang="oobas"> | ||
+ | Do While A > 10 | ||
+ | ' ... loop body | ||
+ | Loop | ||
+ | |||
+ | While A > 10 | ||
+ | ' ... loop body | ||
+ | Wend | ||
+ | </syntaxhighlight> | ||
== Programming Example: Sorting With Embedded Loops == | == Programming Example: Sorting With Embedded Loops == | ||
Line 118: | Line 143: | ||
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. | ||
− | < | + | <syntaxhighlight lang="oobas"> |
Sub Sort | Sub Sort | ||
Dim Entry(1 To 10) As String | Dim Entry(1 To 10) As String | ||
Line 151: | Line 176: | ||
End Sub | End Sub | ||
− | </ | + | </syntaxhighlight> |
− | 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 [ | + | 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 [https://en.wikipedia.org/wiki/Bubble_sort Bubble Sort]. |
− | {{InterWiki Languages BasicGuide|articletitle=BASIC Guide/Loops}} | + | |
+ | {{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Loops}} | ||
{{PDL1}} | {{PDL1}} |
Latest revision as of 11:12, 30 January 2021
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
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:
Const d1 = 2 Const d2 = 3 Const d3 = 2 Dim i Dim a(d1, d2, d3) For Each i In a() ' ... Inner part of loop Next i
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):
- 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). |