7.1.2 Do...Loop
- Purpose
-
Flow control statement that iterates a sequence of statements as long as a specified condition is true, or until a specified condition becomes true.
- Syntax
Do [Statements] [Exit Do] [Statements] Loop [{While | Until} Condition]
- Arguments
-
- Statements
-
Write one or more statements to be iterated while Condition is true or until it becomes true. To write more than one statement, put a line feed after each statement.
- Condition
-
Write a conditional expression that evaluates to true or false.
- Description
-
The Do...Loop statement iterates a sequence of statements as long as Condition is true with the keyword While, or until Condition becomes true with the keyword Until.
The Exit Do statement can only be used within a Do...Loop control structure as a means of exiting the Do...Loop under a condition other than specified in Condition. You can write any number of Exit Do statements anywhere in the Do...Loop. Exit Do is often used with the evaluation of some condition (If...Then statement, for example) to pass control to the statement immediately following the Loop statement.
You can nest Do...Loop statements by placing one Do...Loop within another. Execution of an Exit Do in a nested structure enables escape from the innermost loop enclosing the Exit Do.
Unlike the While...End statement, a sequence of Do...Loop statements is executed at least once in a post-test structure. If you want to evaluate a condition before all the statements are executed, use a While...End statement.
- Example
' Reverse returns the value of the inverted string. ' "EDCBA" is stored in result. result = Reverse("ABCDE") MessageBox(result) Function Reverse(chrValue) Dim chrString, chrLength cnt = 0 chrLength = Len(chrValue) Do chrString = chrString + Mid(chrValue, chrLength - cnt, 1) cnt = cnt + 1 Loop While(cnt < chrLength) Reverse = chrString End Function
- JP1/Script version
-
Supported from JP1/Script 05-10.