Conditional Loops
Conditional loops provide the ability to run a block of code through a loop when the number of loop iterations are not known until run time. There are three different types of conditional loops that provide their own unique features for different circumstances.
- While
- The While loop allows you to execute a block of code while the specified express holds true. The following example increments a variable by 1 while it is less than a 1000:
Number = 2
While Number < 1000
Number = Number + 1
Wend
MsgBox number
- Do While
- A feature with a standard While loop is that if its expression is false to begin with, then the loop will never execute. However, if you require for the loop to execute at least once (even if the conditional expression is false), then use a Do While loop as such:
Number = 1000
Do
'although the expression is already false, 'the loop will still execute once Number = Number + 1
Loop While Number < 1000
MsgBox Number
- Do Until
- A Do Until loop behaves the same way as a Do While loop, except that instead of running while the conditional expression is true it will run until the expression is true. The following demonstrates this:
Number = 1
Do
Number = Number + 1
Loop Until Number > 1000
MsgBox Number
Copyright © 2021. Cloud Software Group, Inc. All Rights Reserved.