basic.if

Statement/BASIC Program, Releases: AP and R83

Initiates logical expression.

Syntax

if logical.expression [ then | else ] statement.block if logical.expression then statement.block [ else statement.block ]

Description

tests the result of a logical expression. Depending on whether the expression evaluates to either 'true' or 'false', the statement(s) following the 'then' or 'else' clauses respectively are executed.

The statements may be placed on a single line (single-line format) or in a block structure (multi-line 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 semicolons.

The single-line format:

if logical.expresssion then statements {else {statements}

where 'statements' represents {statement{; statement{;...}

The single and multiline formats may be combined. Thus, the general format of the multiline statement takes on three forms as shown below:

form 1:

if logical.expresssion then statements else
statements
.
.
end

form 2:

if logical.expresssion then
statements
.
.
end else statements

form 3:

if logical.expresssion then
statements
.
.
end else
statements
.
.
end

form 4:

if logical.expresssion else
statements
.
.
end

form 5:

if logical.expresssion then
statements
.
.
end

If the logical.expresssion evaluates to non-zero, it is considered true. If it evaluates to 0, it is considered false.

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 may be tested by using 'and' and 'or' connectives. No matter how complex the overall expression may be, each condition eventually evaluates to a simple true or false.

Many statements accomodate the 'then/else' construct, and more information is available under the topic, 'then/else construct'.

Example

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

See Also

Command Name Type Description
basic.statements Definition Definition of statements and functions.
basic.search.for.truth Definition Evaluation of null and zero.
basic.then/else.construct Definition provide program flow change based upon a boolean condition.
basic.not Function Negates a logical expression.
basic.match Relational Operator Pattern matching function.
basic.rewind Statement Rewinds magnetic media.
basic.logical.expressions Definition application of logical (Boolean) operators to relational or arithmetic expressions.
basic.null Statement No-op compiler directive.
basic.readnext Statement Retrieves the next item-id from an active list.
statement.block Definition one or more statements that follow a Pick/BASIC decision-path related token such as, but not limited to, 'then', 'else', 'locked' and 'onerr'.
basic.open Statement Opens file for input and output.
basic.else Statement In-line initiator.
basic.writet Statement Writes a string to magnetic media.
basic.= Assignment Operator Logical 'equal' operator or variable assignment.
basic.! Logical Operator Logical 'or' operator and remark line indicator.
basic.case Statement Initiates the case statement.
branching Definition
basic.ifr Statement Initiates a multi-attribute logical expression.

User Comments

What do you think?

Share your experience or ask a question by using the form below.

Login to leave your comments.