DECISIONS AND BRANCHING
by Guy Cousineau


Decision making is a very important part of BASIC programs and is often the cause of tedious gymnastics. This article will cover 5 BASIC statements dealing with decisions and subroutines.

The IF statement precedes most definitions. It is followed by a mathematical or logical statement which can be simple or very complex:


IF x

IF x = 2

IF x^2 >= 2*(y+z)

IF x=2 AND y=3


When BASIC encounters an IF statement it evaluates the expression on the left and compares it to the expression on the right. If the condition is TRUE, the rest of the line is executed. If the expression is FALSE then the rest of the line is ignored. Note in the first example that there is no expression on the right; it is evaluated as X NOT EQUAL TO 0.

The IF statement is followed by THEN and another statement. Let's say I want to make sure that x is never bigger than y:


IF x > y then x = y


Note that IF THEN works on the rest of the physical line and not only on the rest of the statement:


IF x > y then x = y : z = 5


In the line above, z will be set to 5 ONLY when x>y. This approach can help simplify programs and reduce the amount of jumping around required.

Sometimes the amount of work to be done cannot be expressed on a single line; in other cases a decision may be required to determine which routine to execute next. In those cases, the IF statement is followed by GOTO:


100 IF x = 1 GOTO 150

110 IF x = 2 GOTO 200

120 IF x = 3 GOTO 250

130 GOTO 300

150 y=5

160 GOTO 300

200 y=22

210 GOTO 300

250 y=77

300 more program


Note that the 3 IF statements at the beginning which branch to 3 different areas based on values of x. Note also that THEN is not required: IF...GOTO is sufficient and actually executes faster than IF...THEN GOTO. The example above, however shows how programs can become needlessly complicated. Look at the following version of the same decision routines:


100 IF x=1 then y=5

110 IF x=2 then y=22

120 IF x=3 then y=77

300 more program


We have just taken a 10 line program and changed it to 4 lines which look neater.

Let's consider something a bit more complex like MENU decisions. Assume we have 5 major functions which are selected by entering a number from 1 to 5. Here's where ON...GOTO can come in handy. An ON statement looks up a list of line numbers and decides which one to use based on the DECISION variable:


ON x GOTO 100,250,375,465,555


In the example above, the program will jump to line 100 when x=1, to 250 when x=2, and so on. What happens if x is greater than 5? No jump is made and the program falls through to the next instruction. Considering the MENU options selection stated above, the program line following the ON...GOTO could print one of those NASTY messages like "option 1 to 5, silly". Note also that ON works on INTEGER values only and that it starts at a value of 1. The ON statement can be followed by a complex mathematical formula if required:


ON INT(x/3)+1 GOTO 100,200,300,400,500


Note also that ON...GOTO will continue with the next statement (not the next physical line. For experienced programmers, this can be used instead of IF...GOTO to concatenate several lines together even if decisions are required. Consider the following line:


ON x=2 GOTO 500 : y=y+5 : GOTO 700


Here, a jump is made to line 500 whenever x=2 (just like IF...GOTO). However, when x<>2, the rest of the line is executed (unlike IF). Note that when 'x=2' is true, a value of 1 is returned thereby executing the first jump in the list; when the expression is false a 0 is returned and no jump is made.

SUBROUTINES can help reduce program size by executing repetitive functions from a central location. Say you have a MONEY program that prints Dollars and Cents. You spend considerable time developing a technique for right-aligning your figures, making sure there are always 2 digits after the decimal, and preceding the value with a dollar sign. This routine will be several lines long and you will want to access it from various areas in the program. What you do is set up the routine at line 1000:


999 REM print x in dollars and cents.

1000 IF x=0 GOTO 1100

1010 IF x<0 GOTO 1200

1020 ....

1399 RETURN


The routine ends with RETURN which marks the end of the subroutine. Whenever you want to use the routine, you just place the required value in x and GOSUB:


150 x=123.45

160 GOSUB 1000

170 more program


Line 150 sets the value to be decoded, and line 160 instructs your program to execute the subroutine at line 1000. When the RETURN instruction is reached, the program RETURNS where it came from and continues, in this case, with line 170. Note that GOSUB need not be on a line by itself: the RETURN instruction will go back to the NEXT statement in the line:


150 x=123.45:GOSUB 1000 : x=y/2 : GOSUB 1000


The line above will do just what you expect: It will first print $123.45, then it will print one half of the value of y.

Note also the REM statement BEFORE the start of my subroutine. It is a good idea to remind yourself what a routine does and how it does it. This can be indispensable later on when changing your program; don't let a failing memory disable your programs. Not also that the REM statement is BEFORE the routine and not the first statement in the routine itself. This technique will help make your programs run faster since the REM statement does not need to be read every time the routine is executed.

In some cases, you may want to abort from a subroutine to go elsewhere. Say I am using the routine above to show how much money you have left in a game. If you ever run out of money, I want the program to exit. Here's where the POP instruction comes into play:


999 REM print x in dollars and cents.

1000 IF x<=0 GOTO 1100

1020 ....

1100 POP

1110 PRINT "no money left"

1120 GOTO 100


The POP instruction at line 1100 tells BASIC to discard the RETURN address it had stored. Then, program control is returned to line 100 where you might check scores, ask to play again, etc. It is essential to POP correctly in order to maintain all your pointers for the same reasons that using GOTO a subroutine will result in RETURN WITHOUT GOSUB.

Remember IF and ON above? They can also be used quite effectively with GOSUB as well:


IF x=3 THEN GOSUB 1000: y=4 :?"Hello"

ON x GOSUB 1000,2000,3000 : Print x+y


In the first example, the GOSUB instruction will be executed only when x=3; upon RETURN from the subroutine, the rest of the line will be executed. In the second example, the subroutine calls will be made for x=1 x=2 and x=3; for nay value of x, however, the x+y value will always be printed. Note that the RETURN from ON GOSUB comes immediately after the line number list.

Routine Addresses:

IF executes at 7705 (1E19H). It starts by reading the equation and skips the rest of the line if the equation is false.


GOTO executes at 8342 (2096H). It gets the line number, finds the line, sets run mode on, and executes the line (if found). Note that GOTO does not initialize variables as does RUN. It is therefore possible to re-enter a program that has crashed by using GOTO and a strategic line number in the immediate mode. Note that the line number chosen must not be inside a loop or in a subroutine.


ON executes at 8381 (20BDH) and does all sorts of gymnastics to determine the entry in the table to be executed. It then branches to GOSUB or GOTO as required to complete the execution.


GOSUB executes at 8427 (20EBH). It saves the RETURN address on the stack as well as the stack address in IX. It then branches to GOTO to jump to the required routine.


RETURN executes at 8477 (21DDH). It simply reloads the STACK POINTER with the address of the control routine and jumps back to it.


POP executes at 8493 (212DH). It checks if a GOSUB was active and re-adjusts the pointers as required. If there was no GOSUB in progress, an appropriate error message is printed.

As these routines are all very complex, it is not advisable to mess with them. The only interesting patch is to allow formulas in GOTO or GOSUB:


10 FOR x=0 to 7

20 READ y

30 POKE 8342+x,y

40 POKE 8437+x,y

50 NEXT : END

60 DATA 0,0,0,205,3,39,68,77


Line 30 installs the patch for GOTO and line 40 for GOSUB. What the patch does is replace the routine that gets a NUMBER from the command line with a routine that evaluates an EQUATION from the command line.


 

Back to Top