HP 3000 Manuals

IF THEN or IF THEN ELSE [ HP Business BASIC/XL Reference Manual ] MPE/iX 5.0 Documentation


HP Business BASIC/XL Reference Manual

IF THEN or IF THEN ELSE 

The IF THEN or IF THEN ELSE statement executes a "then clause" if the
evaluated numeric expression is TRUE (nonzero).  If the evaluated numeric
expression is FALSE (zero), the IF THEN statement transfers control to
the statement immediately following it, and the IF THEN ELSE statement
executes an "else clause." Each clause is either an executable statement
or a line identifier.  If it is a line identifier, then execution
transfers control to that line.  The syntax of this statement requires
that the entire statement be contained on one line.  The statement can
also be used as a command.

Syntax 

     IF num_expr THEN then_clause [ELSE else_clause]

Parameters 

num_expr         A numeric expression considered TRUE if it evaluates to
                 nonzero and FALSE if it evaluates to zero.

then_clause      Executable program statement or line_id.  If num_expr is
                 TRUE (nonzero), the IF THEN statement executes the
                 executable program statement or transfers control to
                 line_id.  line_id is the line number or line label to
                 which control transfers.

else_clause      Executable program statement or line_id.  If num_expr is
                 FALSE (zero), the IF THEN ELSE statement executes the
                 executable program statement or transfers control to
                 line_id.  line_id is the line number or line label to
                 which control transfers.

Examples 

     10 IF A=B THEN C=3          !Contains executable statement (C=3)
     20 IF X=Y THEN GOTO 40      !Contains executable stmt (GOTO 40)
     21 IF X=Y THEN 40           !Contains line identifier (40)
     30 IF J<>0 THEN Initialize  !Contains line identifier (Initialize)

Lines 20 and 21 above are equivalent.

IF THEN ELSE Construct 

The IF THEN ELSE construct is an alternate form of the IF THEN ELSE
statement.  The IF THEN, ELSE, and ENDIF keywords define a construct that
executes one statement or set of statements if a numeric expression is
TRUE (nonzero) and another statement or set of statements if that numeric
expression is FALSE (zero).

Syntax 

                                      [ELSE              ]
                                      [[else_clause_stmt]] {ENDIF }
IF num_expr THEN [then_clause_stmt]...[.                 ] {END IF}
                                      [.                 ]
                                      [.                 ]

Parameters 

num_expr         A numeric expression that is considered TRUE if it
                 evaluates to nonzero; FALSE if it evaluates to zero.

then_clause_     One or more program lines that execute if num_expr is
stmt             TRUE. These statements comprise the THEN clause.

else_clause_     One or more program lines that execute if num_expr is
stmt             FALSE. These statements comprise the ELSE clause.

After either the IF or ELSE clause executes, control transfers to the
line following the ENDIF statement.

Examples 

     10 IF I THEN                    !The IF THEN portion of the construct
     20    PRINT "I IS NOT ZERO"     !The THEN clause statement
     30 ELSE
     40    PRINT "I IS ZERO"         !The ELSE clause statement
     50 ENDIF

     20 IF A=B THEN
     30   PRINT "A=B"                !The THEN clause statements --
     40   PRINT A                    !will execute if A = B
     50 ELSE
     60   PRINT "A<>B"              !The ELSE clause statements --
     70   PRINT A,B                  !will execute if A <> B
     90 ENDIF

A statement in the IF or ELSE clause can transfer control out of the IF
THEN ELSE construct.

     105 IF (K+J)*I=0 THEN
     110   PRINT "OK"
     115   GOTO 200             !Control transfers to line 200
     120 ELSE
     125   PRINT "NOT OK"
     130   GOSUB Error-routine  !Control transfers to Error-routine
     135 END IF

IF THEN ELSE constructs can be nested; that is, the IF or ELSE clause of
one IF THEN ELSE structure can contain another IF THEN ELSE construct.
The ENDIF is associated with the most recently preceding IF THEN ELSE
construct.

     100 IF I THEN                        !Begin outer construct
     110   IF J THEN                      !Begin inner construct
     120     PRINT "I and J are not 0"
     130   ELSE                           !ELSE for inner construct
     140     PRINT "J is 0 but I is not"
     150   ENDIF                          !End inner construct
     160 ELSE                             !ELSE for outer construct
     170   PRINT "I is 0"
     180 ENDIF                            !End outer construct

The ELSE clause can be omitted.

     406 IF Number_left THEN
     407   PRINT "There are numbers left"
     408   STOP
     409 END IF

Control transfer into a THEN or ELSE clause, but this is not a
recommended programming practice.



MPE/iX 5.0 Documentation