ELSE (BASIC 3.5)

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: ELSE
Abbreviation: E, SHIFT+L
Type: Keyword part
Token code: $d5 (213)
Handling routine
in BASIC ROM:
List of all BASIC keywords


Remark: This article describes the BASIC keyword ELSE in Commodore BASIC V3.5 or higher.

Type: Keyword
General Programming-Syntax:
 IF <condition> THEN <statements_or_line_number>:ELSE <alternative_statements_or_line_number>
 IF <condition> GOTO <line_number>:ELSE <alternative_statements_or_line_number>

The keyword ELSE opens an alternate branch of an IF-THEN- or IF-GOTO construction, respectively. In case the iF expression <condition> gives a logically false (value 0) the BASIC interpreter skips the line until the ELSE token and continues with the following part. This has to be contained in the same line after a THEN or GOTO, respectively. The keyword has to be preceded by at least one colon (besides optional filling spaces). Without a colon the ELSE part will be ignored until the end of line.
If an ELSE is found, the following statements or block <alternative_statements_or_line_number> is processed or the execution branches to the given line number, respectively. In case of a true condition the ELSE part is skipped.
Multiple appearances of ELSE in the same line are ignored.

With BASIC 3.5 nesting of an IF-THEN-ELSE construction is not possible (just in a simple cascading scheme). With BASIC 7.0 a BEGIN-BEND block might span multiple lines. The nesting is some kind of limited and does not conform to usual higher programming languages, since the ELSE branch is the only common part used from all preceding IF-THEN structures:
10 IF A=1 THEN PRINT "A=1": IF B=1 THEN PRINT "A UND B = 1" ELSE PRINT "A NOT 1 OR B NOT 1"
Both of the IFs take the ELSE branch in case the condition is false. Logically this is in the first place equivalent to
10 IF A=1 AND B=1 THEN PRINT "A AND B = 1" ELSE "A NOT 1 OR B NOT 1"
The following assignment shows how to assign an ELSE branch to the corresponding IF (here the inner-most one):
10 IF A=1 THEN PRINT "A=1": BEGIN IF B=1 THEN PRINT "A AND B = 1" ELSE PRINT "B NOT 1": BEND
(or the outer one)
10 IF A=1 THEN BEGIN PRINT "A=1": IF B=1 THEN PRINT "A AND B = 1": ELSE: BEND: ELSE PRINT "B NOT 1"
Even with a appropriate bracketing the outer case needs special attention: The inner IF has to have an ELSE-branch otherwise the inner part IF B=1 could break-out, ignoring a BEND of the outer ELSE branch, which is belonging to IF A=1! The empty inner ELSE branch mitigates this situation.

The token ELSE must not be precede a IF-THEN/GOTO (on places where statements are possible). Any following statements will be ignored until the end of line (similar to the behavior of a REM).

Renumbering a program with RENUMBER considers also sequences like ELSE <line>.

Examples[edit | edit source]

10 INPUT "DIVIDEND, DIVISOR";D1,D2
20 IF D2 THEN PRINT "QUOTIENT:";D1/D2:ELSE PRINT "DIVISOR 0 IS NOT ALLOWED!"

This example takes the dividend and divisor, calculating the quotient. A division by 0 is prevented with a IF-THEN-ELSE construction.

IF A=B THEN PRINT "A=B":ELSE IF A=C THEN PRINT "A=C":ELSE PRINT "A<>B AND A<>C"

Cascading: If A is equal B, A=B is displayed, otherwise it will be checked if A is equal C. If true, then A=C will be shown, otherwise A<>B AND A<>C.


Special cases

30 ELSE PRINT "WILL BE IGNORED"

The statement following the ELSE will be ignored.

100 IF A <= 100 GOTO 500 : ELSE PRINT "LIMIT EXCEEDED!"

is identical with ...

100 IF A <= 100 THEN 500 : ELSE PRINT "LIMIT EXCEEDED!"

are identical, except the branch parts are swapped and condition is negated ...

100 IF A > 100 THEN PRINT "LIMIT EXCEEDED!" : ELSE 500

... where branches are used as usual as for a IF-THEN/GOTO-ELSE construction.


BASIC V3.5 Commands

ABS | ASC | ATN | AUTO | BACKUP | BOX | CHAR | CHR$ | CIRCLE | CLOSE | CLR | CMD | COLLECT | COLOR | CONT | COPY | COS | DATA | DEC | DEF | DELETE | DIM | DIRECTORY | DLOAD | DO | DRAW | DS | DS$ | DSAVE | END | EL | ELSE | ER | ERR$ | EXIT | EXP | FN | FOR | FRE | GET | GET# | GETKEY | GOSUB | GOTO | GRAPHIC | GSHAPE | HEADER | HELP | HEX$ | IF | INPUT | INPUT# | INSTR | INT | JOY | KEY | LEFT$ | LEN | LET | LIST | LOAD | LOCATE | LOG | LOOP | MID$ | MONITOR | NEW | NEXT | ON | OPEN | PAINT | PEEK | POKE | POS | PRINT | PRINT USING | PRINT# | PUDEF | RCLR | RDOT | READ | REM | RENAME | RENUMBER | RESTORE | RESUME | RETURN | RGR | RIGHT$ | RLUM | RND | RUN | SAVE | SCALE | SCNCLR | SCRATCH | SGN | SIN | SOUND | SPC | SQR | SSHAPE | STOP | STR$ | SYS | TAB | TAN | TRAP | TROFF | TRON | UNTIL | USR | VAL | VERIFY | VOL | WHILE | WAIT