The if statement tests the result of a logical expression. Depending on whether the expression evaluates to either true or false, the statements following the then or else clauses, respectively, are executed.
Single-line format
if logical.exp then statement.block if logical.exp else statement.block if logical.exp then statement.block else statement.block
Multi-line format
Form 1
if logical.exp then statements else statements . . end
Form 2
if logical.exp then statements . . end else statements
Form 3
if logical.exp then statements . . end else statements . .end
Form 4
if logical.exp else statements . . end
Form 5
if logical.exp then statements . . end
logical.exp | Expression to be tested.
|
then statement.block | Executes the specified statements if the logical expression evaluates to nonzero. |
else statement.block | Executes the specified statements if the logical expression evaluates to 0. |
Complex conditions can be tested by using the and and or connectives. Regardless of the complexity of the overall expression, each condition eventually evaluates to a simple true or false.
NULL statements are often included in then or else clauses as placeholders. A NULL statement performs no action. However, you can use NULL statements to make the logic of a conditional more clear and easier to read.
If the then clause is omitted, the statement would make sense to the compiler, but it might not be clear to the user. The NULL statement is used to make clear to the user that if the match returns a value of true, no action is taken.
Many statements accommodate the then/else construct. More information is available at then/else statement blocks.
Example 1
if answer = ’exit’ then stop if answer = ’y’ then print ’ok’; cnt=cnt+1; goto 20 if answer = ’y’ then print ’ok’ cnt=cnt+1 end if answer = ’exit’ then gosub 100 else gosub 200 if answer = ’y’ then print ’ok’;cnt=cnt+1 else print ’no good’;stop if answer = ’y’ then print ’ok’ cnt=cnt+1 end else print ’no good’ end
Example 2
IF NUM(PRICE) THEN NULL END ELSE PRINT "ERROR: NON-NUMERIC PRICE. STOP" STOP END
! logical operator, = assignment operator, Boolean evaluation, Branching, case statement, else clause, ifr statement, Logical expressions, match relational operator, not() function, null statement, open statement, readnext statement, rewind statement, statement blocks, Statements and functions, then/else statement blocks, writet statement