IF, ELSE, ELSE IF, ENDIF ( Expression )
The IF, ELSE, ELSE IF, ENDIF ( Expressions ) instructions provide structured conditional program control using Logical/Conditional Expressions. The IF instruction evaluates an expression and executes the associated instruction block when the expression is TRUE. One or more ELSE IF instructions can evaluate additional expressions when preceding IF or ELSE IF expressions are FALSE. An optional ELSE instruction identifies the instruction block to execute when all preceding expressions are FALSE. The ENDIF instruction terminates the structured IF block.
|
|
|
|
|
|
|
|
... one or more PL/B instructions | |
|
|
|
|
|
|
|
|
... one or more PL/B instructions | |
|
|
|
|
|
|
|
|
... one or more PL/B instructions | |
|
|
|
|
|
Where:
label
Optional. A Program Execution Label.
expression
Required. Required for the IF and ELSE IF instructions. A Logical/Conditional Expression enclosed within parentheses that is evaluated as TRUE or FALSE.
Flags Affected: Function Key
Note the following:
The expression specified with an IF or ELSE IF instruction is evaluated as TRUE or FALSE. If the expression is TRUE, the instruction block associated with that IF or ELSE IF instruction is executed.
If an IF expression is FALSE, program control proceeds to the next ELSE IF instruction, the ELSE instruction, or the ENDIF instruction, as applicable. ELSE IF expressions are evaluated in program order until one evaluates TRUE.
The ELSE instruction is optional. Its instruction block is executed when the IF expression and all preceding ELSE IF expressions evaluate FALSE.
The ENDIF instruction terminates the IF/ELSE IF/ELSE structured instruction block.
Logical/Conditional Expressions, including numeric and string comparisons, logical operators, pattern-matching operators, operator precedence, and TRUE/FALSE evaluation, are described in the Expressions section.
ELSEIF may be used in place of ELSE IF.
Each IF structure must have a corresponding ENDIF structure. The ELSE and ELSE IF keywords are optional.
If ELSE IF is used, multiple tests may be executed.
If a FILEPI instruction is active, the FILEPI count is not decremented by this instruction.
IF/ELSE structures may be nested within other IF/ELSE or LOOP/UNTIL structures as long as they do not cross the boundary of the structures they are nested within.
Program Control Instructions (such as GOTO, CALL, BRANCH, etc.) may be placed within an IF/ELSE structure. However, they alter the structure's flow.
IF/ELSE structures may be nested thirty-two (32) levels deep. On compilation listings, the level of the nesting is indicated in the left most column of the listing.
Example:
If the result of dividing '5' into RESULT is 10, the message "RESULT equal to 10" is displayed. Otherwise, the message "RESULT not equal to 10" is displayed.
Example:
Another way of accomplishing the above test would be:
This saves one PL/B instruction and checks the same conditions. However, RESULT does not contain the modified value after this instruction.
Example:
The message "VALID ANSWER" is displayed if the variable DIM1 is equal to 'A' or 'B'. Else, the message "INVALID ANSWER" is displayed.
Example:
The message "VALID ANSWER is displayed if DIM1 is equal to 'A' and FORM2 is equal to '0' or if DIM2 is equal to 'B' and FORM2 is equal to 1.
Example:
If the expression (A+B/10) is not equal to zero, the message "NOT ZERO" is displayed. Otherwise, the message "ZERO" is displayed.
Examples:
See Also: GOTO, Program Control Instructions Expresions
![]() |