ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

IF...THEN...ELSE Command

Summary

Executes a block of code if an expression evaluates to true. May execute an alternate block of code if the expression evaluates to false. (May alternatively perform a GOTO to a specified line number or line lable if the expression evaluates to true.)

Syntax

  • IF expression THEN LineNumber

  • IF expression THEN LineLabel

  • IF expression THEN
        code block
    END IF
    

  • IF expression THEN
        code block
    ELSE
        code block
    END IF
    

Remarks and Examples

A boolean expression is a comparison, or a combination of operators and operands (values), that can be determined to be either true or false at a given point in a program.

Operators that can be used in boolean expressions are the following:

Operator Description Example
= equals X% = 5
<> not equals X% <> 5
< less than X% < 5
<= less than or equals X% <= 5
> greater than X% > 5
>= greater than or equals X% >= 5

Expressions can use numeric (integer and floating-point) values, or string values, but string and numeric values cannot be directly compared with each other.

The IF...THEN statement tests an expression. If the expression uses variables, the expression uses the current values for those variables.

If the expression is determined to be true, then the primary action is performed. The primary action can be a block of code, consisting of one or more statements, and which is ended with the END IF statement, like this:

100 INPUT "Please enter a number: ", N%
110 IF N% <= 10 THEN
120     PRINT "This is a small number"
130 END IF 
140 PRINT "Done."

Sample run 1:

Please enter a number: 5
This is a small number
Done.

Sample run 2:

Please enter a number: 11
Done.

It is also possible to specify a block of code to be executed when the expression evaluates to false:

100 INPUT "What is 2 and 2? ", Answer%
110 IF Answer% = 4 THEN
120     PRINT "No, 2 and 2 is 22."
130 ELSE
140     PRINT "No, 2 + 2 = 4."
150 END IF

Sample run 1:

What is 2 and 2? 4
No, 2 and 2 is 22.

Sample run 2:

What is 2 and 2? 22
No, 2 + 2 = 4.

Sample run 3:

What is 2 and 2? 555
No, 2 + 2 = 4.

There is an alternate form, where a line number or label can be specified immediately after the THEN statement. This causes a GOTO jump to be performed to that line number or label. At present, this form does not allow an ELSE condition.

Example:

100 INPUT "Please enter your name: ", Name$
110 IF Name$ = "Bob" THEN 140
120 PRINT "I'm sorry, I can't tell you the secret number."
130 END
140 PRINT "Welcome, Bob. The secret number is 42."
150 END

Sample run 1:

Please enter your name: Hortensia
I'm sorry, I can't tell you the secret number.

Sample run 1:

Please enter your name: Bob
Welcome, Bob. The secret number is 42.

Some BASIC dialects let you specify one or more statements on the same line after the THEN statement, like this:

IF A$ = B$ THEN PRINT "Yes" : GOSUB 400

This is not currently supported by ReadyBASIC. However, you can use colons and an END IF statement to achieve the same effect:

IF A$ = B$ THEN : PRINT "Yes" : GOSUB 400 : END IF

String comparisons

When comparing two strings, the greater-than and less-than operators cause the strings to be compared using the Unicode chart of characters (TODO Matz: explain and provide link). In general, this means that strings are compared using alphabetical order, but upper-case letters come before lower-case letters, and numeric digits come before upper-case letters. The Unicode character chart shows how other typographical characters and non-English letters are ordered.

IF "Beta" > "Alpha" THEN
    PRINT """Beta"" comes after ""Alpha"""
ELSE
    PRINT """Alpha"" comes after ""Beta"""
END IF
IF "Beta" > "alpha" THEN
    PRINT """Beta"" comes after ""alpha"""
ELSE
    PRINT """alpha"" comes after ""Beta"""
END IF

Result:

"Beta" comes after "Alpha"
"alpha" comes after "Beta"

Comparing numeric and string values

Numeric and string values cannot be directly compared, but you can convert a string to a numeric value using the VAL function, and you can convert a numeric value to a string using the STR$ function. For example:

N$ = "123"
IF VAL(N$) = 123 THEN : PRINT "Yes" : END IF

More complex expressions

You can use the logical operators AND and OR to combine multiple simple expressions into a compound expression. You can also use parentheses to ensure that expressions are evaluated in a particular order.

For example:

A$ = "Test"
B = 234.56
IF A$ = "Test" AND B > 100 THEN
    PRINT "Test 1 is true"
ELSE
    PRINT "Test 1 is false"
END IF
IF (A$ = "Hi" OR A$ = "Bye") AND B > 100 THEN
    PRINT "Test 2 is true"
ELSE
    PRINT "Test 2 is false"
END IF

Result:

Test 1 is true
Test 2 is false

AND has a higher precedence than OR. TODO Matz: Explain precedence/order-of-operations in more detail.

Note that some programmers who are familiar with other programming languages like to wrap the expression in an IF statement with an extra pair of parentheses, even when the extra parentheses are not necessary. There is nothing wrong with this, but it is not required.

Expressions involving floating-point comparisons

Floating-point values are stored internally in such a way that equality comparisons between two floating-point values may sometimes give unexpected results. For example, two seemingly identical values may not be determined to be equal due to rounding.

X = 10 / 3
IF X = 3.33333 THEN : PRINT "Yes" : ELSE : PRINT "No" : END IF

Result:

No
TODO Matz: Explain in more detail

Nesting IF statements

It is possible to nest IF-THEN statements. For example:

IF A > B THEN
    IF C$ < D$ THEN
        PRINT "1"
    ELSE
        PRINT "2"
    END IF
ELSE
    IF X$ = "Hi" THEN
        PRINT "3"
    ELSE
        PRINT "4"
    END IF
END IF

See also: END IF




Copyright 2006-2012, Kevin Matz, All Rights Reserved.