:WHILE... :ENDWHILE
The :WHILE script statement defines a loop. Use a :WHILE block when you need parts of a script to execute several times until a condition is no longer met.
While a particular condition applies, the :WHILE statement loops a process in the script. The condition is a logical expression that supplies either a true or false result. When the condition is true, the statements in the loop are processed. The condition is re-evaluated and if the condition is still true, the statements are processed again. This process repeats until the condition is no longer true, in which case the loop ends and the script continues with the line that follows the :ENDWHILE statement.
Syntax
:WHILE condition
[statements]
:ENDWHILE
Parameters
-
:WHILE
Starts a conditional block -
condition
Expression that defines the condition for the block to continue being processed
Format: script literals, script variables, script functions, or numerical expressions that evaluate as true or false -
statements
One or more script statements that are processed while the condition is true
Format: script statement -
:ENDWHILE
Ends the conditional block
Notes:
- You must include a terminating condition in order to prevent endless loops.
- You can nest an unlimited number of :WHILE blocks.
- A condition can include up to 13 OR operators.
- Script variables can include numbers or strings depending on their declared data type. Numeric variables are treated as numbers in comparisons, even if you use quotation marks. Non-numerical values are compared alphanumerically.
Use the following structure to write conditions:
Value Comparison operator Comparison Value [OR Comparison Value...]
The following comparison operators are available for conditions:
- =or EQ
The expression is true if at least one of the Comparison Values equals Value - <>or NE
The expression is true if none of the Comparison Values equals Value. - < or LT
The expression is true if one of the Comparison Values meets the defined condition. - > or GT
The expression is true if one of the Comparison Values meets the defined condition. - <= or LE
The expression is true if one of the Comparison Values meets the defined condition. - >= or GE
The expression is true if one of the Comparison Values meets the defined condition.
Examples
The following condition is true:
:WHILE "0" = "0"
The following condition is false:
:WHILE "0" = "abc"
The length does not matter when you compare strings. The following condition is true because S comes before W in the alphabet:
:WHILE "Smith" < "Wilson"
The following script statement defines a condition that requires a variable to have one of two possible values in order to be true. If the condition is true, the :WHILE block script lines are processed.
:WHILE &ABT# = "EDP" OR "AE"
The following example requests a user to enter a name and a department. The request is repeated until the user enters a name that is not his or her own name.
:SET &REPEAT# = "Y"
:WHILE &REPEAT# = "Y"
:SET &DEP# = SYS_USER_DEP()
:SET &NAME# = SYS_USER_NAME()
:BEGINREAD
: READ &DEP#, "08" ,"Please enter a department", &DEP#, "M"
: READ &NAME#, "08", "Please enter a name", &NAME#, "M"
:ENDREAD
:IF SYS_LAST_ERR_NR() <> 0
: STOP
:ELSE
: IF SYS_USER_NAME() = &NAME#
: SET &REPEAT# = "Y"
: SET_LAST_ERR 10006, "Please enter a name that is not your own name"
: ELSE
: SET &REPEAT# = "N"
: ENDIF
:ENDIF
:ENDWHILE
See also: