IF statement

Purpose

Test a condition and execute one or more program statements only if the condition is met.

Syntax

IF integer_expression THEN {sub | label | statements} [ELSE {sub | label | statements}]

Remarks

If integer_expression is TRUE (evaluates to a non-zero value), the statements following THEN are executed, and the statements following the optional ELSE are not executed.  If integer_expression is FALSE (zero), the statements following THEN are not executed, and the statements following the optional ELSE are executed.  If the ELSE clause is omitted, execution continues with the next line of the program, provided integer_expression evaluates to FALSE.

integer_expression will often be a result returned by a relational operator as shown here:

IF Income > Expenses THEN x$ = "OK!" ELSE x$ = "Uh-oh"

integer_expression can also be a boolean value.  For example, your program could set the variable BeepOn to 1 (or any non-zero value) if audible beeps are requested, and to 0 if not, then use an IF statement to control output:

IF BeepOn THEN BEEP

…is equivalent to:

IF BeepOn <> 0 THEN BEEP

integer_expression can include the logical operators AND and OR, as in:

IF (a = b) AND (c = d) THEN x$ = "They are equal"

label

If a label is specified, the label must appear within the same Sub, Function, Method, or Property as the IF statement.  The GOTO keyword is implied by THEN, or can replace THEN:

IF EOF(1) THEN GotFile

IF EOF(1) GOTO GotFile

proc

If proc is specified, it must identify a Sub, Function, Method, or Property.

The IF statement and all its associated statements, including those after an ELSE, must appear on the same logical program line.  The following is therefore illegal:

IF a < b THEN t = 15 : u = 16 : v = 17

  ELSE t = 17 : u = 16 : v = 15

…because the compiler treats the ELSE statement as a brand-new statement unrelated to the one above it.  If you have more statements than you can fit on one line, you can use the line continuation  character, the underscore "_", to spread a single logical line over several physical lines.  For example, the following is a legal way of restating the last example:

IF a < b THEN t = 15 : u = 16 : v = 17 _

  ELSE t = 17 : u = 16 : v = 15

A better method of programming long and complex IF/THEN constructs is to use the IF block statement.

Also note that every statement following the ELSE will be executed if integer_expression is FALSE.  For example, you might expect the following statement:

Taxable = %TRUE

Price = 1.00

Rate = .05

Total = 5.00

IF Taxable THEN Tax = Price * Rate ELSE Tax = 0: Total = Total + Tax

…to bring Total to 5.05, but it won't.  The Total = Total + Tax statement will only be executed if Taxable is FALSE.  It's easy to get the correct results using the IF block:

IF Taxable THEN

  Tax = Price * Rate

ELSE

  Tax = 0

END IF

Total = Total + Tax

Short-Circuit Evaluation

 

Note that PowerBASIC features short-circuit evaluation of relational expressions using AND and OR.  This optimization means that evaluation of a relational expression in an IF, IF/END IF, DO/LOOP, or WHILE/WEND is terminated just as soon as it is possible to tell what the result will be.  For example:

IF LEN(a$) AND MyFunc(a$) THEN CALL ShowText("Ok!")

In the above example, if LEN(a$) is zero, there is no further need to evaluate the expression, because 0 and anything will always be FALSE.  So, if LEN(a$) is zero, MyFunc() is not called at all, and ShowText() is not executed.

To give short-circuit optimization an extra boost, AND and OR are treated as a Boolean operator rather than a bitwise operator, and this can sometimes produce unexpected results.  For example, consider the following expression:

a& = 4

b& = 2

IF a& AND b& THEN CALL ShowText("TRUE") ELSE CALL ShowText("FALSE")

Applying the traditional BASIC bitwise evaluation, you would expect to see FALSE displayed because (4 AND 2) = 0.  Due to the short-circuit optimization though, each value is treated as a Boolean, just as if you had written:

IF ISTRUE a& AND ISTRUE b& THEN ...

If you believe this may be a problem for your particular code, you can disable the short-circuit evaluation by surrounding the entire conditional expression in parentheses:

IF (a& AND b&) THEN CALL ShowText("TRUE") ELSE CALL ShowText("FALSE")

The parentheses force the entire expression to be evaluated, so AND reverts to being a bitwise operator.

See also

CHOOSE, CHOOSE&CHOOSE$IF block, IIF, IIF&, IIF$, MAX, MAX&, MAX$, MIN, MIN&, MIN$, SWITCH, SWITCH&, SWITCH$, SELECT

Example

IF x > 100 THEN y = 3 ELSE y = 4