THE BEGINNERS PAGE
Robert Alonso, Assistant Editor
Logical Dreams
"If" is one of the most useful words in our vocabulary; it allows us to test situations and make appropriate decisions based on the test. Likewise, the BASIC command IF is one of the most useful words in the computer's vocabulary, and for the same reasons. Computer programs always rely on logical decisions to produce a result. Everything from data processing applications to arcade-style games relies on IF-THEN testing. The format is pretty simple: if something is true, then do something in response. For example, IF joystick is pushed up, THEN move spaceship up.
Using The Right IF
The IF-THEN statement can often be replaced with the statement IF-GOTO. Before mixing the statements or replacing one with the other in your programs, you must first understand the nature of each. IF-THEN is the most convenient and safest to use of the two. The reason for this is that almost any instruction placed after the THEN will be executed without any problems. You can place a line number after the THEN and the program will go to that line number, or you can place an expression such as A = A + 1 and the program will execute it. The IF-THEN statement can thus be a very powerful and useful part of your programs. IF-GOTO is not as versatile as IF-THEN because it can only execute line numbers after the GOTO. If you tried placing an expression such as the previously mentioned A = A + 1, the computer would flag it as an error.
IBM's Double GOTO
The Apple, Atari, and Commodore computers all flagged Program 1 as having an error in the line containing the IF-GOTO. The only two computers tested that did not flag it as an error were the IBM PC and PCjr. The IBM computers allowed the expression A = A + 1 after an IF-GOTO and also allowed the following line:
20 IF A = l GOTO GOTO 40
The double GOTO was allowed only if a line 40 had been entered and only after the IF statement. A program with a line number followed by double GOTOs and a target line number resulted in an error. This kind of rule bending is atypical of IBM. Just for reference you should know that the second edition (May 1982) of the BASIC manual by IBM and Microsoft states: "If the expression is true (not zero), the THEN or GOTO clause is executed. THEN may be followed by either a line number for branching or one or more statements to be executed. GOTO is always followed by a line number."
Although IBM may let you get away with an expression after an IF-GOTO, you should try to avoid such a construction within your programs. It is not standard and can produce errors and plenty of confusion. It is probably better to use only the IF-THEN statement because it allows either an expression or a line number and works the same on all the tested computers.
Sometimes an IF-THEN construction alone is not enough. In some situations, a structure called IF-THEN-ELSE can be useful. This structure is quite similar, but allows you to specify two THEN outcomes (one that's triggered by the IF and one that's triggered by an implied "IF NOT"). In other words, if the condition following the IF is true, whatever follows the THEN is carried out. If it is false, whatever follows the ELSE is carried out.
The Missing ELSE
However, this IF-THEN-ELSE construction is almost never used in programs published in magazines and books. The reason for this is not that there is something better, but that many home computers (Apple, Atari, and Commodore) do not have an ELSE command as part of their BASIC. IBM is one of the few that do allow IF-THEN-ELSE. The TI Extended BASIC cartridge also allows it.
There is a way to mimic IF-THEN-ELSE. Let's say that you want to test if a variable is equal to 100 and you want the THEN to end the program if it is. Otherwise, you want an ELSE to add 1 to the variable and let the program continue. Program 2 is an example of a routine that will do just that, without the ELSE command.
Imitating IF-THEN-ELSE
The IF-THEN-ELSE construction is in lines 30 and 40. The reason this works is that if the IF-THEN in line 30 is false, program execution "falls through" to line 40. The line following an IF-THEN can thus be used for the ELSE. There are some extra precautions that you should take. If the IF-THEN in line 30 had an expression (like A = A + 1) instead of the END instruction, the program would execute the expression and then go to the next line and execute the line which you are using as an ELSE. This must be avoided or your program will not work properly. Program 3 is an example of how to properly mimic an IF-THEN-ELSE.
Take a look at the differences between Programs 2 and 3. The GOTO 50 in line 30 of Program 3 prevents the program from going on to line 40 when the IF condition is true. You should always include a GOTO with a target line number at the end of your IF-THEN if you are going to create the IF-THEN-ELSE construction. It is the only way to insure that the ELSE condition will not be executed haphazardly.
Program 1: IF-GOTO Error Demo
10 A = l 20 IF A = l GOTO A = A + 1 30 PRINT A
Program 2: IF-THEN-ELSE construction
10 A=0: B=0: REM INITIALIZE 20 B=B+A: REM EXPRESSION 30 IF A = 100 THEN END: REM IF THEN 40 A=A + 1: GOTO 20: REM ELSE
Program 3: Better IF-THEN-ELSE
10 A=0: B=0: REM INITIALIZE 20 B = B + A: REM EXPRESSION 30 IF A = 100 THEN PRINT B: GOTO 50: REM IF THEN 40 A = A + 1: GOTO 20: REM ELSE 50 END