The Do...Loop statement iterates for as long as (or until) its boolean test expression is True. The boolean test expression can appear at the beginning or the end of the Do...Loop statement.
The following syntax applies to Do...Loop statements with the test at the beginning of the loop.
Do <While | Until> <expression> <statements> Loop
The following example uses the Do While...Loop construct.
Dim counter As Integer = 0 Dim total As Integer = 0 Do While counter < 10 total = total + counter counter = counter + 1 Loop
Alternatively, the previous example could have used Do Until instead of Do While, in which case the Do Until statement would have been expressed as:
Do Until counter = 10
The following syntax applies to Do...Loop statements with the test at the end of the loop.
Do <statements> Loop <While | Until> <expression>
When the test expression appears at the end of the Do...Loop statement, the statement block within the loop is executed at least once.
The following example is similar to the previous Do While...Loop example, except that it uses the Do...Loop While construct.
Dim counter As Integer = 0 Dim total As Integer = 0 Do total = total + counter counter = counter + 1 Loop While counter < 10
Alternatively, the previous example could have used Loop Until instead of Loop While, in which case the Loop Until statement would have been expressed as:
Loop Until counter =10
You can jump out of a Do...Loop statement by using an Exit statement, as shown in the following example.
In this example, a parameter specifies the weight per unit of some item. A Do...Loop statement inside the load rule adds the weight to a total load (of up to 10 units). The load has a limit of 25. As additional unit weights are added, the total load is compared to the limit and a system message is generated. If the load exceeds the limit, a warning is added to the message, and the iteration stops. Control passes out of the Do...Loop statement.
Parameter Rule unitWeight As Integer = 5 Rule load As Number Dim counter As Integer = 1 Dim limit As Number = 25.0 Dim strItem As String = " item." Do If counter > 1 Then strItem = " items." End If load = load + unitWeight If load > limit Then printValue("Warning: Load = " + stringValue(load) + _ "kg for " + stringValue(counter) + strItem + " " + _ stringValue(limit) + "kg limit exceeded!") Exit Do Else printValue("Load = " + stringValue(load) + _ "kg for " + stringValue(counter) + strItem) End If counter = counter + 1 Loop While counter <= 10 End Rule