In a programming language, loops allow you to execute a <block of code> a number of times depending on loop exit condition. In Overbasic there are 3 types of loops: FOR, DO UNTIL, WHILE. The peculiarity that distinguishes them is the condition to exit the loop, that is the condition that ends the loop execution.
FOR loop #
The FOR loop allows you to execute the <block of code> it contains for a specified number of times.
Syntax
For <counter> = <start> To <end> [Step <increment>]
<block of code>
Next [<counter>]
<counter> is a Numeric variable declared before the loop.
<start> represents the initialization value of <counter>.
<end> represents the final value of <counter>, the value that ends the loop.
<increment> (required only if <start> is greater than <end>) represents the <counter> increment at each loop iteration. If not specified, its default value is 1.
How it works:
<counter> is initialized with <start> value. At each iteration it is incremented by <increment> value until it reaches <end> value. When <counter> reaches <end> value, loop ends.
Notes:
- If <increment> is NOT specified, its value is 1.
- If <start> is greater than <end>, it is mandatory to define <increment> using STEP keyword. In this case, <increment> must be negative otherwise the loop will not be executed even once.
- <counter> can be omitted after NEXT keyword.
- If <counter> is changed within the FOR loop, the change does NOT affect the number of loop iterations.
Examples:
'Calculate the factorial of 5 -> (5!)
Dim factorial as Numeric = 1
Dim i as Numeric
For i = 1 To 5
factorial = factorial * i
Next i
'Result:
'i = 1; factorial = 1 * 1 = 1
'i = 2; factorial = 1 * 2 = 2
'i = 3; factorial = 2 * 3 = 6
'i = 4; factorial = 6 * 4 = 24
'i = 5; factorial = 24 * 5 = 120 (end loop)
'Example with negative STEP
Dim result as Numeric = 1
Dim i as Numeric
For i = 5 To 1 Step -1
result = result * i
Next i
'Result:
'i = 5; result = 1 * 5 = 5
'i = 4; result = 5 * 4 = 20
'i = 3; result = 20 * 3 = 60
'i = 2; result = 60 * 2 = 120
'i = 1; result = 120 * 1 = 120 (end loop)
DO UNTIL loop #
The DO UNTIL loop allows you to execute the <block of code> it contains as long as the loop exit condition is FALSE. When the exit condition becomes TRUE, loop ends.
Syntax:
Do Until <condition>
<block of code>
Loop
Example:
'Calculate the factorial of 5 -> (5!)
Dim factorial as Numeric = 1
Dim i as Numeric = 1
Do Until i > 5
factorial = factorial * i
i = i + 1
Loop
'Result:
'i = 1; factorial = 1 * 1 = 1
'i = 2; factorial = 1 * 2 = 2
'i = 3; factorial = 2 * 3 = 6
'i = 4; factorial = 6 * 4 = 24
'i = 5; factorial = 24 * 5 = 120
'i = 6; end loop
WHILE loop #
The WHILE loop allows you to execute the <block of code> it contains as long as the loop exit condition is TRUE. When the exit condition becomes FALSE, loop ends.
Syntax:
While <condition>
<block of code>
EndWhile
Example:
'Calculate the factorial of 5 -> (5!)
Dim factorial as Numeric = 1
Dim i as Numeric = 1
While i <= 5
factorial = factorial * i
i = i + 1
Loop
'Result:
'i = 1; factorial = 1 * 1 = 1
'i = 2; factorial = 1 * 2 = 2
'i = 3; factorial = 2 * 3 = 6
'i = 4; factorial = 6 * 4 = 24
'i = 5; factorial = 24 * 5 = 120
'i = 6; end loop
Nested loops #
The <block of code> of a loop can contain other loops (of any type). At each iteration of the main loop, inner loops will be fully executed.
Example:
Dim a as Numeric = 0
Do Until a > 10
Dim i as Numeric
For i = 1 To 5
a = a + 1
Next i
Loop
'Result: a = 15
EXIT and EXIT ALL #
EXIT and EXIT ALL keywords are used within a loop to exit the loop prematurely without waiting for the exit condition. The difference between EXIT and EXIT ALL is that EXIT exits only the loop it is in, while EXIT ALL exits all nested loops to continue program execution starting with the next statement at the end of the outermost loop.
Examples:
For i = 1 To 10
Do Until x > 5
If (i = 9) Then
Exit All
EndIf
If (Open(x) > Close(x)) Then
Exit
EndIf
x = x + 1
Loop
'EXIT will continue from here
...
...
Next i
'EXIT ALL will continue from here
...
...