IF

From C64-Wiki
Jump to: navigation, search
BASIC keyword
Keyword: IF
Abbreviation: -
Type: Command
Token code: -/$-
Handling routine
in BASIC ROM:
-–-
$-–-
List of all BASIC keywords


Remark: This article describes the BASIC-Command IF in BASIC V2 at the Commodore 64.

Typ: Command
General Programming-Syntax: IF <equation> THEN <linenumber> 
                               or IF <equation> GOTO <linenumber> 
                               or IF <equation> THEN <command>

The BASIC command IF is used together with the BASIC command THEN or with the BASIC command GOTO by condition.

When the condition is true, the BASIC commands after THEN or the GOTO command will be done. When the condition is false then the BASIC interpreter will ignore the rest of the BASIC commands in the line.

For the condition the following relational operators are useful:

  • = equal
  • <> unequal
  • < less than
  • > greater
  • <= less-equal
  • >= greater-equal

Furthermore, logical operators like AND, OR or NOT can be used to combine several conditions, and parentheses () to override precedence order.

Examples[edit]

10 PRINT CHR$(147);
20 INPUT "2 numbers, please: ";A$, B$
30 IF A$="" OR B$="" THEN 20
40 A=VAL(A$): B=VAL(B$)
50 IF A>0 THEN PRINT "A is positive and ";
51 IF A<0 THEN PRINT "A is negative and ";
52 IF A=B THEN PRINT "A is equal to B ";
53 IF A<>B THEN PRINT "A is not equal to B ";
54 IF NOT A AND NOT B THEN PRINT ", neither number is -1";
55 IF A=64 AND B=64 THEN PRINT " !!! BINGO !!!": GOTO 58
56 IF A=B AND A=0 GOTO 58
57 IF (A AND B=0) OR (NOT B AND A=0) THEN END
58 FOR X=0 TO 3000: NEXT
59 GOTO 10

Technically what happens is the expression is evaluated, and if the expression is 0, the statements after THEN or GOTO are not executed. If the expression is non-zero, the statements after THEN or GOTO will be executed. So something like this can be used:

10 IF A THEN PRINT "A is nonzero"
BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π  |POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT