WHILE
Enables repeated execution of a block of command.
Syntax
WHILE [expression]
commands
ENDWHILE
Parameters
expression | A logic expression. If expression includes fields from the current set, then it is evaluated using the values of the specified fields in the current member of the set. |
ENDWHILE | Marks the end of the WHILE structure. A WHILE command must always have a corresponding ENDWHILE. If a necessary ENDWHILE is missing, all subsequent commands in the current procedure are treated as part of the WHILE structure, and all of those commands continue to be executed as long as expression is true. When the end of the procedure is reached, the “open” WHILE command is closed. |
Comments
The WHILE … ENDWHILE structure enables a block of commands to be repeatedly executed, as long as expression is logically true.
As soon as the software detects that expression is logically false, execution resumes at the first command following the ENDWHILE. To exit a loop before expression evaluates to false, use a BREAK command.
An open WHILE loop is automatically closed if
- you exit a procedure in the middle of a WHILE structure (using a BYE, RETURN, STOP, or TRANSFORM command), or
- a procedure ends before the ENDWHILE is encountered.
Example
% Commands to select and display form fDispEmp
%
——————————————————————-
find all Employees % Create set of all Employees
while $setcount > 0 % Loop until end of set
change fDispEmp from current % Fill form with emp. data
form display nolabel input % Display form and start input
if Event.EventName = “Escape” % Check for Escape key
return % If so, finish
endif
if ThisForm.FormChanged = $true % Check if data modified
change current from fDispEmp % Update current member
endif
next % Go to next record
let $setcount = $setcount – 1 % Decrement $setcount
endwhile
while Age > 20
… other commands …
% Missing endwhile statement
… more commands …
last command in procedure
In the preceding example, the commands subsequent to the missing ENDWHILE command are executed as if they were a part of the WHILE structure. When the end of the procedure is reached without encountering the necessary ENDWHILE, it generates an error message.
form open fCustomers
while
form display input
… more commands …
if Event.EventName = “Escape”
break
endif
…more commands …
endwhile
Notice in the preceding command that a BREAK (or BYE, RETURN, STOP, or TRANSFORM) command is required to exit a WHILE loop that contains no logic expression.