If...Then statements
Applies to
There are three forms of the If statement:
- Single-line If;
- Multi-line If; and
- If as an expression. (See "If Conditional Expression")
Each form is discussed separately.
If conditional expression
Description
Same as a single-line If statement, except that the entire statement is evaluated as an expression, leaving only one of two values.
Syntax
If test Then true_expression [Else false_expression]
- OR -
If test Else false_expression
Parameters
The conditional If expression has the following parameters.
Parameter | Description |
---|---|
Test | An expression that equates to a Boolean value ¾ true (non-zero) or false (zero). |
true_expression, false_expression | The conditional-expression If allows the test for a condition where a statement cannot be used (in other words, where only a single line is available, and it must evaluate as an expression). It returns one of two values, depending on the result of the test expression. |
When test is true, then the value of true_expression is the result of the If expression. When the test is false (0 or null) the value of the false_expression is the result of the If expression. For example:
@ANS = If {CHANGED} Then "YES" Else "NO"
The dictionary record that contains the above code is used in a report that shows when a record has been changed. If the CHANGED field is not empty and contains anything other than a 0, then "YES" will be printed in the report.
The conditional-expression If must be written on a single line. The Then and Else clauses may only contain an expression.
Example
Function MAX (X,Y) MAX=If X>Y Then X Else Y Return MAX
Single-line If statement
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.
Parameter | Description |
---|---|
Condition | An expression that equates to a Boolean value ¾ true or false. False is 0 or null. True is any other value. |
Statements | Any 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. */
Multi-line If statement
Syntax
If test Then statements End Else statements End </pre>
Parameters
The multi-line If statement has the following parameters.
Parameter | Description |
---|---|
test | An expression that equates to a Boolean value ¾ true or false. False is 0 or null. True is any other value. |
statements | Any valid BASIC+ statement. May include branching instructions, such as GoTo or GoSub. |
Then or Else | When Then or Else is the last keyword on a line, BASIC+ assumes that multiple lines follow. The Else clause is optional. One or more statements may appear in the Then or Else clauses. They may be written on the same line, if separated by semicolons. |
End | Use End to terminate the multi-lined Then statements and the multi-lined Else statements. Should you fail to include the End statement to indicate the end of the Then or Else logic, the program will not compile, or, it will use the next available End statement as the termination of your Then or Else logic, rendering unpredictable results. |
Other Forms of Multi-line If statements
Syntax
If test Then statements End
- OR -
If test Then statement Else statements End
- OR -
If test Then statements End Else statement
- OR -
If test Else statements End
Example
*Using multiple-line If statements /* If the value of INV_COUNT is more than 144, the program will transfer to subroutine GROSS. If the value of INV_COUNT is less than or equal to 144, the program transfers to subroutine NOTGROSS for reordering. */ If inv_count GT 144 Then GoSub GROSS End Else GoSub NOTGROSS End