IF/ENDIF Examples


Example 1:

 

  

IF

F1

  

DISPLAY

  *N,"F1 was hit";

  

ENDIF

 

 

The message "F1 was hit" is not displayed unless the F1 (Function Key 1) Function Flag is TRUE.

 

Example 2:

 

  

IF

F1

  

DISPLAY

*N,"F1 was hit";

  

ELSE

 

  

IF FKEY

 

  

DISPLAY

*N,"Another Function or ":

  

  

"Control Key was hit";

  

ELSE

 

  

DISPLAY

*N,"No Function or ":

  

  

"Control Key was hit";

  

ENDIF

 

  

ENDIF

 

 

The message "F1 was hit" is displayed if the F1 (Function Key 1) Function Flag is TRUE. If any other Function or Control Key was struck the message "Another Function or Control Key was hit" is displayed. Otherwise, the message "No Function or Control Key was hit" is displayed.

 

Example 3:

 

  

IF

F1

  

DISPLAY

*N,"F1 was hit";

  

ELSE IF

F2

  

DISPLAY

*N,"F2 was hit";

  

ELSE IF

F3

  

DISPLAY

*N,"F3 was hit";

  

ELSE IF

FKEY

  

DISPLAY

*N,"Another Function or ":

  

  

"Control Key was hit";

  

ELSE

 

  

DISPLAY

*N,"No Function or ":

  

  

"Control Key was hit";

  

ENDIF

 

 

The message "F1 was hit" is displayed if the F1 (Function Key 1) Function Flag is TRUE. The message "F2 was hit" is displayed if the F2 (Function Key 2) Function Flag is TRUE. If any other Function or Control Key was struck the message "Another Function or Control Key was hit" is displayed. Otherwise, the message "No Function or Control Key was hit" is displayed.

 

Example 4:

 

  

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

 

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 6:

 

  

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

 

  

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 8:

 

  

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.

 

 



PL/B Language Reference IF Example IFILE Examples