6 Branching and Subroutines

Structured Programming

Structured programming techniques improve the programming task via better program organization.

Structured IF THEN ELSE

The structured IF statement allows program execution to resume at one of two points depending on the result of a test expression. The syntax for the structured IF is as follows:

IF conditional expression THEN

(statements)

.

.

.

ELSE

(statements)

.

.

.

END IF

The ELSE statement is optional. If left out and the conditional expression is false, execution is passed to the statement after END IF. If a branch occurs into the statements immediately after IF, program execution continues after the END IF when the ELSE is encountered. When an END IF is required but cannot be found, an error occurs.

Here is an example sequence from a text processing program. It checks the first character of string Line$. If it contains a period, the statements immediately following IFTHEN determine the exact string and branch to perform the appropriate function. If the first character is not a period, the ELSE statements are executed.

200  IF Line$[1,1]="." THEN        ! Check for command line.
210    IF Line$[1,3]=".BR" THEN Break
220    IF Line$[1,3]=".PA" THEN Page
230    IF Line$[1,3]=".AP" THEN Append
240  ELSE
250    PRINT Line$                 ! Print text line.
260    Printpos=Printpos+1
270  END IF
      .
      .
      .

The WHILE Block

The WHILE . . . END WHILE statements allow repeated execution of a series of statements while a conditional expression remains true. This condition is evaluated at the beginning of the loop. If the expression is initially false, the loop is bypassed without ever being executed.

The syntax for the block is as follows:

WHILE conditional expression

(statements)

.

.

.

END WHILE

If control is transferred into the loop via GOTO or other non-structured construct, an error results when the END WHILE is encountered. If an END WHILE cannot be found when required, an error occurs.

Here is a sequence which reads and prints numbers in successive records of a disk file. Control exits the block when the TYP function returns 3, indicating that the end of file is reached.

10   ASSIGN #1 TO "Parts, SYSTEM"
20   WHILE TYP(1)3             !Exit at end of file.
30     READ #1:Part$
40     PRINT Part$
50   END WHILE
60   END

The LOOP Block

The LOOP block allows repeated execution of a series of statements until an explicit request is made to exit. The statements between LOOP and END LOOP are executed until an EXIT IF statement terminates the loop. The syntax for the block is as follows:

LOOP

(statements)

.

.

.

END LOOP

The loop may be exited only by an EXIT IF statement.

EXIT IF conditional expression

If the condition is true, the loop is exited. If control is transferred into the loop via GOTO or another non-structured construct, an error results when the END LOOP is encountered.

Note in the next example that EXIT IF exits the inner-most structured block. Other structured blocks active within LOOP (for example, REPEATs, WHILEs, or FORs) are deactivated by removal from the system stack.

10  DIM Ltr$[1],Char$[1]
20  INTEGER Xpos,Badguess,Goodguess
30  DATA 31,T,33,E,35,L,37,O,40,B,42,O,43,C
40  DISP " Guess the statement by entering one letter at a time:",LIN(2)
50  DISP SPA(30);"_H_ _O_P _LK"
60  RESTORE
70  READ Xpos,Ltr$
80  LOOP
90    CURSOR (Xpos,4)                    !  Put cursor at current blank.
100   INPUT "";Char$[1;1]
110   IF Char$=Ltr$ THEN
120     DISP " Good Guess!"
130     WAIT 1000
140     DISP "   "
150     EXIT IF Ltr$="C"                 ! Exit loop if last guess is C.
160     READ Xpos,Ltr$
170     Goodguess=Goodguess+1
180   ELSE
190     BEEP
200     DISP "Wrong letter ... try again."
210     WAIT 1000
220     DISP "   "                       ! Clear message line.
230     CURSOR (Xpos,4)
240     Badguess=Badguess+1
250   END IF
260 END LOOP
270 DISP "You guessed it within";Goodguess+Badguess;"tries,";
280 DISP "and";Badguess;"incorrect guesses!"
290 END

The REPEAT Block

The REPEAT and UNTIL statements allow repeated execution of a series of statements until a certain condition is true. This condition is evaluated at the end of the loop. The loop is always executed at least once. The syntax for the block is as follows:

REPEAT

(statements)

.

.

.

UNTIL conditional expression

If control is transferred into the loop via a GOTO or other non-structured construct, an error results when UNTIL is encountered.

The next sequence repeats lines 50 and 60 until X=Xsqr.

10   INPUT "Enter an integer >1:";X
20   Xsqr=X*X
30   DISP "Sum of";X;"through";Xsqr;"is:";
40   REPEAT
50     Sum=Sum+X
60     X=X+1
70   UNTIL X=Xsqr
80   DISP Sum
90   END

The SELECT Block

The SELECT block allows any of a variety of blocks of statements to be executed depending on the value of a selection expression. The syntax of the SELECT block is as follows:

SELECT string or integer selection expression

CASE case list

(statements)

.

.

.

CASE ELSE

(statements)

.

.

END SELECT

The case list is a list of case items separated by commas. A case item is defined as follows:

or

Any number of CASE statements may be used. CASE ELSE is optional. At syntax time, all constants in the case list are verified to be either type string or integer. At execution time, a check is made to verify that the type of selection expression matches the types in the CASE statements. Some example CASE statements are shown here:

100 CASE 1
100 CASE <5
100 CASE >59
100 CASE 1 TO 10
100 CASE "A" TO "J"
If the selection expression matches the ranges specified in any of the CASEs, the statement block following that CASE is executed. If no CASE is matched, the statement block following the CASE ELSE is executed. If there is no CASE ELSE, the SELECT block is entirely bypassed. Any CASEs following the first CASE ELSE are ignored.

If control is passed into the SELECT block via GOTO or other non-structured construct, the first CASE or CASE ELSE encountered causes control to transfer to the line following END SELECT. Statements following the SELECT, but preceding the first CASE or CASE ELSE, are not executed unless control is specifically passed to them via GOTO or another non-structured construct.

The next program sequence traps many errors typically encountered in a text processing program. The SELECT BLOCK defines a display message to explain each error. The Disperr routine inserts the error message at the current cursor position in test.

1000 Err_msgs:    !Display error message and wait for ENTER key.
1010     BEEP
1020     SELECT ERRN
1030     CASE 18
1040       Line1$="INPUT LINE IS TOO LONG."
1050       Line2$="HIT ENTER, EDIT AND RE-INPUT THE LINE."
1060     CASE 53
1070       Line1$="IMPROPER FILE NAME FORMAT."
1080       Line2$="HIT ENTER AND RE-SPECIFY FILE NAME."
1090     CASE 56
1100       Line1$="FILE NAME IS UNDEFINED."
1110       Line2$="HIT ENTER AND RE-SPECIFY FILE NAME."
1120     CASE 132 TO 134
1130       Line1$="PRINTER OFF-LINE OR SWITCHED OFF."
1140       Line2$="READY PRINTER AND PRESS ENTER TO CONTINUE."
1150     CASE ELSE           ! Other errors trapped here.
1160       Line1$=ERRM$
1170       Line2$="CALL SYSTEM MANAGER FOR HELP."
1180       WAIT              ! Wait for softkey.
1190     END SELECT
1200     GOSUB Disperr
1210     IF (CURKEY=5) OR (CURKEY=6) THEN RETURN
1220     GOTO Start
1230 Disperr:   !
1240     Ypos=YPOS
1250     CURSOR (1,Ypos)
1260     DISP "  "     ! Make room for message in text.
1270     CURSOR (1,Ypos),IV(80)
1280     DISP Line1$
1290     DISP Line2$
1300     INPUT         ! Wait for ENTER key.
1310     CURSOR (1,Ypos)
1320     DISP "  "     ! Delete message.
1330     CURSOR (1,Ypos)
1340     RETURN

Eloquence Language Manual - 19 DEC 2002