6 Branching and Subroutines

Conditional Branching

The IF THEN Statement

The IF THEN statement is used to provide branching which is dependent on a specified condition. Syntax for this statement is as follows:

IF numeric expression THEN line id

NOTE: Working with line number is not recommened and doesn't work in the IDE.

If the numeric expression has a value other than 0, it is considered true and branching occurs to the specified line id. If its value is 0 (false), execution continues with the line following the IF THEN statement. For example:

10   INPUT A
20   IF A THEN 50
30   PRINT "A=0"
40   GOTO 10
50   PRINT "A=";A
60   END
The IF THEN statement is used most often with relational operators. For example:

100   INPUT "HOURS WORKED?";Hours
110   IF Hours>40 THEN Overtime
120   DISP "NO OVERTIME ENTERED."
130   GOTO 100
140  Overtime:  Over=Hours-40
150             PRINT "OVERTIME PAY =";Over*Pay*1.5
160             GOTO 100
Another form of the IF THEN statement provides conditional execution of a statement without branching. Syntax is as follows:

IF numeric expression THEN statement

When the value of the numeric expression is not 0 (true), the statement is executed. When the value of the numeric expression is 0 (false), execution continues with the following line. For example:

200   READ X,Y
210   IF X=Y THEN PRINT "X=Y"
220   PRINT "X=";X,"Y=";Y
A special expression exists for User Defined Types.

IF Instance_name IS A Type_name THEN

With this expression it is possible to find out if the given Instance has has been derived from the given Type. If the given Instance and the given Type have been derived from the same Base_type, then the expression is true, too.

Example:

IF My_car IS A Car THEN CALL Write_car(STRUCT My_car)

All executable Eloquence statements are allowed following THEN.

The following statements are not allowed after THEN since they are declaratory statements, not executable statements:

COM                 INTEGER
DATA                OPTION BASE
DIM                 REAL
DEF FN              REM
FN END              SHORT
END                 SUB
IMAGE               SUBEND
Here is another example use of IF THEN, which branches to one of many routines depending on the input response.

10   INPUT "DO YOU WISH DAILY, WEEKLY, OR MONTHLY REPORTS?";Report$
20   IF UPC$(Report$[1,1])="D" THEN Daily
30   IF UPC$(Report$[1,1])="W" THEN Weekly
40   IF UPC$(Report$[1,1])="M" THEN Monthly
50   DISP "INCORRECT ENTRY"
60   WAIT 2000
70   GOTO 10
80 Daily:  ! Print daily report.
 .
 .
 .
130         STOP
140 Weekly:  !  Print weekly report.
 .
 .
 .
220         STOP
230 Monthly: !  Print monthly report.
 .
 .
 .
300 Continue: ! Continue program.
 .
 .

Eloquence Language Manual - 19 DEC 2002