IB Statements/if

From CometWiki

Jump to: navigation, search

IF statement

Syntax: IF relational-expression [THEN] conditional-statement(s)

(or)

IF relational-expression [THEN]

conditional-statement(s)

ELSE

conditional-statement(s)

ENDIF


Discussion: There are two forms of the IF statement. The IF/THEN statement evaluates a relational-expression and, if the result is true, executes the conditional-statement(s). If the relational-expression produces a false result, the conditional-statements are not executed; program flow continues with the next statement.

Note: In Internet Basic, the keyword "THEN" is not required. However, for the sake of program readability, we recommend using it.


Like the simple IF/THEN statement, the IF/THEN/ELSE/ENDIF statements begin with the evaluation of a relational-expression. If the relational-expression yields a true result, the conditional-statement(s) on the following source lines are executed.

Unlike the simple IF/THEN statement, multiple conditional-statement(s) are not separated with an ampersand or continued with an underline character.

If the structure contains an ELSE statement, statements up to the keyword "ELSE" are executed if the relational-expression is true. If the relational-expression yields a false result, statements following the keyword "ELSE" up to the keyword "ENDIF" are executed.

The word THEN is optional, but the words ELSE and ENDIF are mandatory and must be the only statements on the source program line.

A relational-expression consists of three elements: a variable name, a relational operator, and a comparison value. For example, to determine if a variable named CRLIMIT has a value greater than 500, you would use the following relational-expression:

 CRLIMIT GT 500

In relational-expressions, the variable name always appears first, followed by a relational-operator from the following list:

 Relational operator      Internet Basic syntax
 =============================================
 Equal to                 EQ  or  =
 Not equal to             NE  or  NOT=
 Greater than             GT  or  >
 Greater than or equal to GE  or  >=
 Less than                LT  or  <
 Less than or equal to    LE  or  <=

The comparison value is the third item in a relational-expression. It may be a constant or variable and must agree in data type with the variable name (i.e., strings must be compared to strings, numbers to numbers).

Compound relational-expressions may be defined with the AND and OR parameters. These link individual relational-expressions -- the outcome is based on the joint outcome as follows:

 Parameter Conditions                         Outcome
 AND       both conditions are true           result is true
 AND       at least one condition is false    result is false
 OR        either condition is true           result is true
 OR        both conditions are false          result is false

In compound expressions containing AND and OR, the AND parameter takes precedence (much like multiplication and division take precedence over addition and subtraction in a compound numeric expression).

If multiple conditional-statements are to be executed, they must be separated with the ampersand character (&). If these statements exceed the length of a single editor line, they must be continued with the underline character (_). For an alternative syntax, see the IF/THEN/ELSE statement.

Example 1: IF FLAG$ = "Y" THEN GOTO 9999


Example 2: IF A=B AND B=C THEN PRINT (0) "VALUES ARE ALL EQUAL."


Example 3: If A=B OR B=C THEN PRINT (0) "SOME VALUES ARE EQUAL."


Example 4: IF OPTION$="QUIT" THEN_

PRINT (0) "NOW ENDING PROGRAM" &_
CLOSE (1) & CLOSE (2) &_
RUN "QMONITOR"


Example 5: IF OPTION$="QUIT" THEN

PRINT (0) "NOW ENDING PROGRAM"
CLOSE (1) & CLOSE (2)
RUN "QMONITOR"

ELSE

PRINT (0) "PLEASE ENTER OPTION AGAIN:"
INPUT (0) OPTION$
GOTO 500

ENDIF

Personal tools