Conditional Statements

Top  Previous  Next

Emergence BASIC has two main conditional statements, IF and SELECT. A conditional statement selects a section of your program to execute based on a condition that you choose.

IF Statement

The IF statement will probably be your most used conditional statement. The statement has two forms, the block IF and a single line IF. The block form executes on or more statements if a condition is TRUE
 

IF condition {THEN}
'Statement(s) to execute if condition is TRUE
ELSE
'Statement(s) to execute if condition is FALSE
ENDIF

The condition is any math, comparison or algebraic expression that results in a yes or no result.  The ELSE keyword is optional and represents the group of statements that will be executed if the condition is false. The THEN keyword in a block IF is also optional and is supported for backwards compatibility with other languages. Every block style IF must be matched with a corresponding ENDIF statement

An example of the IF block statement:
 

OPENCONSOLE
DEF number:INT
INPUT "Pick a number  ",number
IF number = 12
   PRINT "You guessed correctly!"
   PRINT "Your very smart"
ELSE
   PRINT "Sorry wrong number"
ENDIF
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

In the previous Operators page we listed the conditional operators that can be used in an IF statement. Comparisons can be performed on numbers, strings and variables.

The IF statement also supports a single line version using the THEN keyword.  When used on a single line the ENDIF statement is not necessary. Single line IF statements allow only one action be executed when the condition is true and one following an optional ELSE.

Example of a single line IF statement:
 

OPENCONSOLE
DEF number:INT
INPUT "Pick a number  ",number
IF number = 12 THEN PRINT "You guessed correctly!" ELSE PRINT "Sorry wrong number"
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

You can use multiple conditions in an IF statement by using the Boolean operators AND and OR.  The conditions should be enclosed in parenthesis because the Boolean operators have higher precedence. Examples:
 

IF (a < 1) AND (b > 3)
   PRINT "TRUE!"
ENDIF

Will print TRUE! if a is less than 1 AND b is greater than 3.
 

IF (a < 1) OR (b > 3)
    PRINT "TRUE!"
ENDIF

Will print TRUE! if a is less than 1 OR b is greater than 3.

ELSEIF statement

The ELSEIF clause allows combining multiple IF/ELSE/ENDIF statements into a single block.  The condition of each ELSEIF statement is only compared if the previous IF or ELSEIF statement is FALSE.  For example:
 

IF name = "Jerry"
       pay = "7.55"
ELSEIF name = "Tom"
       pay = "9.55"
ELSEIF name = "Lisa"
       pay = "10.34"
ENDIF

The above code is equivalent to:
 

IF name = "Jerry"
       pay = "7.55"
ELSE
       IF name = "Tom"
               pay = "9.55"
       ELSE
               IF name = "Lisa
                       pay = "10.34"
               ENDIF
       ENDIF
ENDIF

As you can see the ELSEIF statement saves a lot of typing.
 

SELECT Statement

The SELECT statement is an advanced conditional statement that allows you to test against many different combinations. The syntax of the select statement is:
 

SELECT expression
    CASE test1
                'Statement(s) to execute if expression = test1 is TRUE
    CASE test2
                 'Statement(s) to execute if expression = test2 is TRUE
    CASE testn
                 'Statement(s) to execute if expression = testn is TRUE
    DEFAULT
                 'Statement(s) to execute if all tests are FALSE
ENDSELECT

The SELECT statement allows unlimited ‘if equal to" conditions.  The statements after the CASE statement will only be executed if the expression is equal to the test condition. Example:
 

OPENCONSOLE
DEF Choice$:STRING
PRINT "Press some keys, Q to quit"
LABEL again
DO
Choice$ = INKEY$
UNTIL Choice$ <> ""
SELECT Choice$
   CASE "A"
   CASE& "a"
            PRINT "You pressed A!!"
   CASE "Z"
   CASE& "z"
            PRINT "Z is my favorite letter!"
   CASE "Q"
   CASE& "q"
            CLOSECONSOLE
            END
   DEFAULT
            PRINT "You pressed the letter ",Choice$
ENDSELECT
GOTO again

The DEFAULT condition is optional and will be executed if none of the CASE statements is true. CASE statements are mutually exclusive. To group conditional tests for the same statements together use the CASE& statement following the initial test CASE.

The SELECT statement can also be used to test multiple conditions and execute the first TRUE case. To do this use SELECT 1 as the opening statement and code each CASE statement as a condition. Example:
 

OPENCONSOLE
DEF Choice:INT
PRINT
LABEL again
INPUT "Enter a Number, 0 to end: ", Choice
IF Choice = 0
   CLOSECONSOLE
   END
ENDIF
SELECT TRUE
   CASE (Choice > 10)
            PRINT "number is greater than 10"
   CASE (Choice < 10)
            PRINT "Number is less than 10"
   CASE (Choice = 10)
            PRINT "Number is equal to 10"
ENDSELECT
PRINT
GOTO again

In this example we test the choice against multiple conditions using only one SELECT statement. Your conditions can be as complex as needed, only the first TRUE condition will be executed so make sure the conditions are unique.