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} 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 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 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) Will print TRUE! if a is less than 1 AND b is greater than 3. IF (a < 1) OR (b > 3) 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" The above code is equivalent to: IF name = "Jerry" 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 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 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 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. |