Do Loops (Basic Syntax)
A Do loop can be used to execute a fixed block of statement an indefinite number of times.
The 4 different types of Do loops
|Type of Do Loop||Explanation||Example|
|Do While … Loop||The Do While ... Loop evaluates the condition, and if the condition is true, then it evaluates the statements following the condition.
When it has finished doing this, it evaluates the condition again and if the condition is true, it evaluates the statements again.
It continues repeating this process until the condition is false.
Do While conditionstatements Loop
|Do Until ... Loop||The Do Until ... Loop is similar to the Do While ... Loop except it keeps evaluating the statements until the condition is true rather than while it is true.|| |
Do Until conditionstatements Loop
|Do ... Loop While||The Do ... Loop While evaluates the statements only once.
It then evaluates the condition, and if the condition is true, evaluates the statements again. This process continues until the condition is false.
Dostatements Loop While condition
|Do ... Loop Until||Similar to Do ... Loop While except that it evaluates the statements until the condition is true.|| |
Dostatements Loop Until condition
Note The Do loops support an Exit Do statement to immediately jump out of the loop. The Exit Do statement is similar to the Exit For in For/Next loops.
Do While ... Loop Formula Example
The following example searches for the first occurrence of a digit in an input string. If a digit is found, it returns its position, otherwise it returns -1. In this case, the input string is set explicitly to a string constant, but it could be set equal to a String type database field instead.
For example, for the input String, "The 7 Dwarves", the formula returns 5, which is the position of the digit 7.
Dim inString inString = "The 7 Dwarves" Dim i, strLen i = 1 strLen = Len (inString) formula = -1 Do While i <= strLen And formula = -1 Dim c As String c = Mid (inString, i, 1) If IsNumeric (c) Then formula = i i = i + 1 Loop