IF .. THEN .. ELSE .. ENDIF

Purpose To make a decision regarding program flow based on the result returned by an expression.
Syntax

IF expression [THEN]
.....
[ELSE]
.....
ENDIF

If the result of the expression is non-zero (logical true), the block of program lines between the THEN and the ELSE statements will be executed. If the result of the expression is zero (false), the block between the IF and ELSE will be ignored, and the block between the ELSE and ENDIF statements will be executed instead.

If there is no ELSE statement, and if the result of the expression is false, the block of program lines between the THEN and the ENDIF statement will be ignored, but execution will continue right after the ENDIF statement.

Nesting of IF statement

Statement blocks within the IF..THEN..ELSE statement may contain other IF..THEN..ELSE blocks (nesting). Note that each IF statement must be ended with the ENDIF statement. Otherwise an error message "IF without ENDIF" will be reported during compilation.

Testing Equality: Special comparison operators may be used in the expression of the IF statement. Only integer expression may be compared. For comparison of strings, please refer to the "STRCMP(A$, B$)" function.

Equal

=

Not Equal

<>

Greater than

>

Less than

<

Greater than or Equal to

>=

Less than or Equal to

<=

Examples

IF A >= B*5-20*C OR C=20
  B = B-1
ELSE
  B = B*3
ENDIF

Comments: A few comparison expressions may be linked with logical-AND (AND statement) or logical-OR (OR statement) operator as shown in the above examples.

backbutton.gif (507 bytes)  Basic to TBASIC Reference Manual