Description

Evaluates the logic of a test expression, and transfers program control to one of two statements, depending on the value (true or false) of the test expression.

Syntax

If condition Then statements [Else statements]

- OR -

If condition Else statements

Parameters

The single-line If statement has the following parameters.

ParameterDescription
ConditionAn expression that equates to a Boolean value ¾ true or false. False is 0 or null. True is any other value.
StatementsAny valid BASIC+ expression. May include branching instructions such as GoTo or GoSub.

If...Then...Else statements provide the framework for conditional branching. The normal program sequence is interrupted by testing a condition and then transferring program control, depending on the outcome of the evaluation. If the condition tested is evaluated as true (non-zero), program control will branch to the Then clause statements. If test is evaluated as false, control will branch to the optional Else clause statements, if present. If test is false and no Else clause is present, program control will move on to the next program statement following the entire If...Then set.

For example:

If X = A+B Then Call WRAPUP Else GoSub MORESTUFF

This program will call subroutine WRAPUP if X is equal to A+B. The program will branch to subroutine MORESTUFF if X is not equal to A+B. The following example contains no Else clause:

If X = A+B Then Call WRAPUP

Here, the program will call subroutine WRAPUP if X is equal to A+B. If X is not equal to A+B, the program will move on to the next program statement following the If...Then set.

More than one statement may be included in the Then or Else clauses; however, the statements must be separated by semicolons (;), as shown in the following example:

If S=500 Then S=20 ; W=1 Else A=S ; W=0.

Example

* Using a single-line If statement.
If X+5 GT Z Then GoSub MAJOR Else GoSub MINOR
/* If X + 5 is greater than Z, the program branches to the internal subroutine, MAJOR. 
Otherwise, the program branches to subroutine MINOR. */
  • No labels