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.
The single-line format:
if logical.exp then statement.block if logical.exp else statement.block if logical.exp then statement.block else statement.block
The multi-line format:
Form 1:if logical.exp then statements else statements . . endForm 2:
if logical.exp then statements . . end else statementsForm 3:
if logical.exp then statements . . end else statements . .endForm 4:
if logical.exp else statements . . endForm 5:
if logical.exp then statements . . end
logical.exp | Expression to be tested. If it evaluates to nonzero, it is considered true. If it evaluates to 0, it is false. |
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. |
Commands can be placed on a single line (single-line format) or in a block structure (multiline format). The two formats are functionally identical.
If more than one statement is contained in either the then or else clause, they must be separated by a semicolon.
The single and multiline formats can be combined.
If there is no clause for the current condition, the next sequential statement following the entire if statement is executed.
The then clause of an if statement is optional if the else clause is present. However, one or the both must be present.
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.
Many statements accommodate the then/else construct. More information is available under the topic, then/else statement blocks.
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
! 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