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.

 

 

[label]

IF

(Expression)

 

 

... one or more PL/B instructions

 

[label]

ELSE IF

(Expression)

 

 

... one or more PL/B instructions

 

[label]

ELSE

 

 

 

... one or more PL/B instructions

 

[label]

ENDIF

 

 

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:

  1. 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.

  2. 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.

  3. The ELSE instruction is optional. Its instruction block is executed when the IF expression and all preceding ELSE IF expressions evaluate FALSE.

  4. The ENDIF instruction terminates the IF/ELSE IF/ELSE structured instruction block.

  5. 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.

  6. ELSEIF may be used in place of ELSE IF.

  7. Each IF structure must have a corresponding ENDIF structure. The ELSE and ELSE IF keywords are optional.

  8. If ELSE IF is used, multiple tests may be executed.

  9. If a FILEPI instruction is active, the FILEPI count is not decremented by this instruction.

  10. 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.

  11. Program Control Instructions (such as GOTO, CALL, BRANCH, etc.) may be placed within an IF/ELSE structure. However, they alter the structure's flow.

  12. 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:

 

       DIV "5",RESULT

       IF (RESULT=10)

        DISPLAY *N,"RESULT equal to 10";

       ELSE

        DISPLAY *N,"RESULT not equal to 10";

       ENDIF

 

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:

 

       IF (RESULT/5=10)

        DISPLAY *N,"RESULT equal to 10";

       ELSE

        DISPLAY *N,"RESULT not equal to 10";

       ENDIF

 

This saves one PL/B instruction and checks the same conditions. However, RESULT does not contain the modified value after this instruction.

 

Example:

 

 

       IF (DIM1="A" | DIM1="B")

        DISPLAY *N,"VALID ANSWER...";

       ELSE

        DISPLAY *N,"INVALID ANSWER...";

       ENDIF

 

The message "VALID ANSWER" is displayed if the variable DIM1 is equal to 'A' or 'B'. Else, the message "INVALID ANSWER" is displayed.

 

Example:

 

       IF ((DIM1="A" & FORM2=0) | (DIM1="B" & FORM2=1))

        DISPLAY *N,"VALID ANSWER";

       ELSE

        DISPLAY *N,"INVALID ANSWER";

       ENDIF

 

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 (A+B/10)

        DISPLAY *N,"NOT ZERO";

       ELSE

        DISPLAY *N,"ZERO";

       ENDIF

 

If the expression (A+B/10) is not equal to zero, the message "NOT ZERO" is displayed. Otherwise, the message "ZERO" is displayed.

 

Examples:

 

       IF ( A > B )

        MOVE "A is greater" TO RESULT

       ELSE

        MOVE "A is not greater" TO RESULT

       ENDIF

 

       IF ( ( A > B ) & ( C <> 0 ) )

        MOVE "Condition is TRUE" TO RESULT

       ELSE IF ( ( A = B ) | ( C = 0 ) )

        MOVE "Alternate condition is TRUE" TO RESULT

       ELSE

        MOVE "All conditions are FALSE" TO RESULT

       ENDIF

 

       IF ( dName = "EDWARD" )

        MOVE "Name matches" TO RESULT

       ELSE IF ( dName LIKE "ED%" )

        MOVE "Pattern matches" TO RESULT

       ENDIF

 

       IF ( NOT ( ( A > B ) & ( C <> 0 ) ) )

        MOVE "Final expression result is FALSE" TO RESULT

       ENDIF

 

 

See Also: GOTO, Program Control Instructions Expresions

 



PL/B Language Reference