WHILE statement creates a loop which is iterated as long as the Boolean expression specified after the WHILE keyword evaluates as true. The syntax of the WHILE statement is as follows:
Element |
Description |
Expression |
A Boolean expression |
Logical Block |
A block of executable statements |
The statements contained in the WHILE loop are iterated while the condition specified in a Boolean expression is TRUE. 4GL evaluates the Boolean expression, if it is TRUE, all the statements within the WHILE loop (up to the END WHILE keywords) are executed, after that 4GL returns to the beginning of the loop and evaluates the expression once more. If it is TRUE, one more iteration is executed, if it is FALSE, the WHILE loop is terminated and the statements that immediately follow the END WHILE keywords are executed. If the value of the Boolean expression is changed to FALSE during the execution of the loop, the loop still will executed till the end, it will be terminated only when 4GL evaluates the Boolean expression once more before the next iteration.
In the example below 4GL continues inserting new rows while the yes_no variable is evaluated as "y". As soon as the user types "n" in the PROMPT field, the WHILE loop is terminated:
LET yes_no = "y"
WHILE yes_no = "y"
CALL insert_row()
PROMPT "Do you want to insert one more row (y/n) : "
FOR yes_no
END WHILE
EXIT WHILE and CONTINUE WHILE statements can influence the execution of the WHILE loop. If the database you work with supports transactions, it is advisable that the complete WHILE loop be placed within a transaction.
Logical block which is included into the IF statement has this structure:
Element |
Description |
|
|
|
|
Declaration clause |
The optional DEFINE statement |
Statement list |
Executable 4GL or SQL statements |
WHILE statement can include only one logical block. In its turn the logical block consists of the Declaration clause and the Statement list.
The Declaration clause contains one or more DEFINE statements and is used to declare spot variables. These variables are visible only within this logical blocks and cannot be referenced from outside the WHILE statement. The DEFINE statement of the Logical Block is optional and can be omitted.
Statement list can include:
If 4GL encounters a CONTINUE WHILE statement, it interrupts the execution of the current iteration of the WHILE loop, comes back to the beginning of the WHILE loop and evaluates the Boolean expression once more. If the expression is true, one more iteration is performed, if it is not, the complete WHILE loop is skipped.
4GL terminates the WHILE loop and continues execution from the statements that follow the END WHILE keywords, when it encounters the EXIT WHILE keywords. All the statements placed between the EXIT WHILE and END WHILE keywords are skipped.
4GL will not be able to exit the WHILE loop, if the value of the Boolean expression is not changed to FALSE during the execution of the loop, unless the EXIT WHILE keywords are specified. To make 4GL leave the WHILE loop, you can also use other logical transfer of the program control, i.e. the GOTO statement.
END WHILE keywords specify the end of the WHILE loop. When 4GL encounters these keywords, unlike in other 4GL statements, it will not execute the statements that follow the END WHILE keywords. 4GL will return to the beginning of the WHILE loop and evaluate the Boolean expression once more. If the expression is TRUE, it will iterate the WHILE loop again. If the expression is FALSE, it will exit the WHILE loop and execute the statements that immediately follow the END WHILE keywords.