Futurebasic/Language/Reference/if

From Wikibooks, open books for an open world
Jump to navigation Jump to search

IF[edit | edit source]

Statement[edit | edit source]

Appearance Standard Console

Syntax[edit | edit source]

IF expr THEN {dest1|statement1 [:statement2 ...]} ¬

  [ELSE {dest2|statement3 [:statement4 ...]}] dest1 and dest2 are either line numbers or quoted statement labels. 

Description[edit | edit source]

Conditionally executes one or more statements, or jumps to an indicated line, based on the value of expr. If expr is evaluated as "true" or as nonzero, then the program either jumps to the line indicated by dest1, or it executes statement1, statement2, etc. If expr is evaluated as "false" or as zero, and you've included the ELSE clause, then the program either jumps to the line indicated by dest2, or it executes statement3, statement4, etc. Each of the statement's can be any executable statement except a "block" statement such as FOR, WHILE, DO, etc. expr can be either a numeric expression, a "logical" expression, or a string. A logical expression normally contains "data comparison" operators, and can be evaluated as being either "true" or "false." Here are some examples of logical expressions: x! > 19.7 myName$ = "RICK" 6*7 <= 42In FB, numeric expressions and logical expressions are interchangeable. When a numeric expression is used in the context of a logical expression, then it's considered "true" if it's nonzero, or "false" if it's zero. For example: IF x+3 THEN BEEP Here, the BEEP will be executed if and only if x+3 is not zero. When a logical expression is used in the context of a numeric expression, then it's evaluated as -1 if it's true, or as 0 if it's false. For example: found = (fileName$ = seekName$) Here, if fileName$ equals seekName$, the value -1 is assigned to found; otherwise, found is assigned a value of 0. You can use the operators AND, OR, NOT within expr. Note, however, that these three are considered to be arithmetic operators, not logical operators. This can lead to some unexpected results if you're not careful. For example, this expression: firstNumber& AND secondNumber& may evaluate to zero (false), even when both firstNumber& and secondNumber& are each nonzero (true). When you wish to use AND, OR, or NOT in the context of a logical expression, you should use operands which always evaluate either to -1 or to 0. For example: firstNumber& <> 0 AND secondNumber& <> 0 This expression behaves "logically," because (firstNumber&<>0) is always -1 or 0; and likewise (secondNumber&<>0) is always -1 or 0. The expr can also be a string. When a string is used in the context of a logical expression, it's evaluated as "true" if and only if the length of the string is greater than zero. Note: The IF statement is a one-line structure. To create a conditional structure spanning multiple lines, use the LONG IF statement. Use caution when comparing floating point values to zero or to whole numbers. The following expression may not evaluate as expected: IF x# = 1In this statement, the compiler compares the value in x# to an integer "1". Since SANE and PPC math both use fractional approximations of numbers, the actual value of x#, though very close to one, may actually be something like 0.99999999 and therefore render unexpected results.

See Also[edit | edit source]

LONG IF; AND; OR; NOT; SELECT CASE