FutureBasic Logo

<<    Index    >> FutureBasic

if   statement



Syntax 1:
if expr then {dest1|statement1 [:statement2 ...]} ¬
   [else {dest2|statement3 [:statement4 ...]}]

Syntax 2:
if expr
   [statementBlock1]
[else
   [statementBlock2]]
end if

Description
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 <= 42

In FutureBasic, 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
Use caution when comparing floating point values to zero or to whole numbers. The following expression may not evaluate as expected:
if x = 1
In 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
and; or; not; select case