IF statement executes statements depending on whether the desired condition is met. It can contain only one block of statements which will be executed only if the condition specified in the statement is met. It can also have two blocks of statements one of which is executed when the condition is met and the other is executed when it is not met.
Expression |
A Boolean expression |
Logical block |
A block that contains variables declaration and executable statements |
The IF statement is used with a Boolean expression. If the Boolean expression specified after the IF keyword is evaluated as TRUE, the statements between the THEN keyword and the ELSE keyword (or between the THEN keyword and the END IF keywords, if the ELSE clause is omitted) are executed. After than the statements that follow the END IF keywords are executed.
If the Boolean expression is evaluated to FALSE, 4GL executes the statements after the ELSE keyword (if it is present). If there is no ELSE block in the IF statement, 4GL skips the statements within the IF statement entirely and starts executing the statements that follow the END IF keyword. The Boolean expression also returns FALSE, if it is evaluated as NULL (unless it is used with the IS NULL operator).
The IF loops can be nested in one another, the limit is about 20 loops, the limit also depends on the number of the FOR and WHILE loops used in the module. If you want to use several IF loops to test the same value against different results, it is advisable that you use the CASE statement instead.
LABEL retry:
IF yes_no MATCHES [Yy]
CALL tot_inf()
ELSE IF yes_no MATCHES [Nn]
MESSAGE "The program will be closed in 1 sec."
SLEEP 1
EXIT PROGRAM
ELSE
MESSAGE "You should enter either Yy for yes or Nn for no."
GOTO :retry
END IF
END IF
Logical block which is included into the IF statement has the following structure:
Declaration clause |
The optional DEFINE statement |
Statement list |
Executable 4GL or SQL statements |
The IF statement can include one logical block, if the ELSE clause is absent or two logical blocks, if it is present. Each of them can have its own declaration clause with the DEFINE statement used to declare spot variables. These variables are visible only within their logical blocks and cannot be referenced from outside the IF statement. The variables declared in the ELSE clause cannot be used outside the ELSE clause. The DEFINE statement of the Logical block is optional and can be omitted.
Statement list can include any executable statements which may use the corresponding spot variables.