IF Statement

Allows conditional execution of a sequence of DataBasic statements.

Syntax

Single line THEN/ELSE

IF expression [THEN statement{;statement}... | ELSE statement{;statement}...]

Single line THEN/Multi-line ELSE

IF expression THEN statement{;statement}... ELSE
statement
.
.
.
END

Multi-line THEN/Single line ELSE

IF expression THEN
statement
.
.
.
END {ELSE statement{;statement}...}

Multi-line THEN/Multi-line ELSE

IF expression THEN
statement
.
.
.
END {ELSE
statement
.
.
.
END}

Multi-line ELSE

IF expression ELSE
statement
.
.
.
END

Syntax Elements

expression Any logical expression.

statement Any valid DataBasic statement.

Conditions

If the test condition specified by expression is true (nonzero), the statement(s) following THEN are executed.

If expression is false (zero), the statement(s) following ELSE are executed. If the ELSE case is omitted, control passes to the next sequential statement following the entire IF statement.

Comments

Any number of valid DataBasic statements can be executed following a THEN or ELSE. Multiple statements can either be included on the same line as the THEN or ELSE word, in which case they should be separated by semi-colons, or they can be on separate lines following the THEN or ELSE, in which case the conditional sequence must be terminated with an END statement. Various combinations can be typed when using the THEN and ELSE clauses together.

Single-line Statements

In a single-line IF statement, one or more statements can follow the THEN and/or ELSE word, but they must be separated by semicolons. For example:

IF ITEM THEN PRINT X; X=X+1 ELSE PRINT Y; GO 5

If the current value of item is true (nonzero), the value of X is printed and incremented by 1. Control then passes to the next statement in the program. If ITEM is false (zero), the value of Y is printed and control transfers to the statement at label 5.

Any statements can appear in THEN and ELSE clauses, including more IF statements.

Multi-line IF Statement

The multi-line IF statement is functionally identical to the single-line IF statement. However, the statement sequences are placed on multiple program lines with each sequence terminated by an END statement.

For every multi-line THEN and ELSE statement, there must be a corresponding END statement. If there are not enough END statements, a compilation error results. If there are too many END statements, the program might compile successfully, but the program may terminate early and not all of the program statements may be compiled.

Examples

IF A="STRING" THEN PRINT "MATCH"

Prints MATCH if value of A is STRING.

IF X>5 THEN IF X<9 THEN GOTO 10

Transfers control to the statement at label 10 if X is greater than 5 but less than 9.

IF Q THEN PRINT A ELSE PRINT B; STOP

Prints the value of A if Q is a nonzero numeric. If Q is zero or null, the value of B is printed and the program terminates. If Q evaluates to a non-numeric string, system message [B16] Non-numeric data when numeric required; zero used.  is displayed.

IF A=B THEN STOP ELSE IF C THEN GOTO 20

Terminates program if A=B; if A does not equal B and C is nonzero, control passes to the statement at label 20.

IF A = 0 ELSE PRINT A

Prints value of A if it is nonzero. If A is zero, control passes to next statement.

IF ABC=ITEM+5 THEN
    PRINT ABC
    STOP
END ELSE PRINT ITEM; GOTO 10

Prints value of ABC and terminates program if ABC=ITEM+5; otherwise, the value of ITEM is printed and control passes to statement 10.

IF X>1 THEN
    PRINT X
    X=X+1
END ELSE
    PRINT "NOT GREATER"
    GOTO 75
END

If X>1, prints value of X, increments X and passes control to statement following the second END; otherwise, prints NOT GREATER and passes control to statement 75.

10 IF S="XX" THEN PRINT "OK" ELSE
        PRINT "NO MATCH"
        PRINT S
        STOP
    END
20 REM REST OF PROGRAM

Prints OK and passes control to statement 20 if S is XX; otherwise, NO MATCH and the value of S are printed, and the program terminates.

IF NMBR=0 ELSE
    PRINT MESSAGE NMBR
    NMBR = 100
END

If NMBR is not equal to zero, prints the value of MESSAGE and NMBR and assigns 100 to NMBR.