If...Then...Else Statement
Language Items List
Definition:
Provides for one-line conditional execution, based on the evaluation of an
expression.
Syntax (1):
If condition Then thenstatements [Else elsestatements]
Syntax (2):
If condition1 Then
[statementblock-1]
[Elself condition2 Then
[statementblock-2] ]
[Else
[statementblock-n] ]
End If
Syntax
In a single-line If...Then...Else statement, it is the value of the condition
that determines if an action will occur. The single-line form of the If...
statement that is shown in Syntax 1 is useful for simple one-line conditional
tests.
The syntax is described as follows:
Syntax Description
If Marks the beginning of the simple If...Then control structure.
condition Can be one of two types of expressions:
- ) An expression that evaluates true (nonzero) or false (zero).
- ) An expression of the form TypeOf object Is objecttype. The object is an object variable and objecttype is the name of an object.
Then Identifies what actions should be taken if condition is met.
thenstatements Statements that are executed when condition is true or if object matches the type specified by objecttype. Can be one or more statements that must be separated by colons. If thenstatements appears on the same line as the IF then the If..Then statement is a simple
If..Then if there are no statements after the Then, the If..Then statement is a
block If..Then.
Else Identifies what actions should be taken if condition is not met. If the Else clause is omitted, control passes to the next program
statement.
elsestatements Statements that are executed when condition is false or if object does not match the specified objecttype. Can be one or more statements that must be separated by colons.
(Syntax 2-specific items)
Elself Indicates that alternative conditions must be evaluated if condition1 is not satisfied.
condition2 Same as condition1 described previously.
statementblock-2 One or more Phoenix statements executed if condition2 is satisfied or if object is the same type specified by objecttype.
Else Identifies what actions should be taken if none of the previous conditions are
satisfied.
statementblock-n One or more Phoenix statements executed if condtion1 and condition2 are both unsatisfied or if object is not the same type specified by objecttype.
End If Ends the block form of the If...Then decision statement.
Note: The If,,,Then,,,Else statement does allow for multiple statements with a
condition. If used, they must be placed on the same line and separated by colons,
as shown in the following example. However, it is recommended that you use the
Multiple-line or block form structure shown in Syntax 2 when multiple statements
are involved.
If Form.Font Then NumFonts = NumFonts + 1 : Print “Found Font”
The multiple-line or block form of If...Then...Else provides more structure
and flexibility than the single-line form. It allows for conditional execution of
multiple statements and should be used when longer, more complex conditional
tests are required. This block form is usually easier to read, maintain, and
debug than the single-line form.
Details:
To determine whether an If statement is a single-line or multiple-line block
form, Phoenix looks at what appears after the Then keyword. If anything other
than a comment appears after Then, the statement is considered a single-line If
statement.
To execute a block If, Phoenix tests condition1 first. If it tests true, all the statement blocks following Then are
executed.
It is valid to combine a “typeof obj is objname” with other expressions, as in: “If TypeOf objRef Is Form || a = 1 Then Print “true”. The logical operators && and || give better results in cases such as this
and are recommended over the bitwise operators (AND and OR) in similar situations.
The logical and bitwise operators are very similar. They all have the same
Truth table, with respect to the values True and False. The difference lies in how
the expression is determined as True or False.
When using &&, the second operand is only evaluated for && if the first part
is True. If the first part is False, a result of False is assumed and the second
part not evaluated. For example:
If form1.Font && form1.Font.Size = 10 Then Print “size = 10”
Similarly, the second operand is only evaluated for || if the first part is
False. This means the second part is checked, since the expression is not yet
known to be True. For example, If a || b then True, if a or b independently are
non-zero the result will be True, the result is not dependent on the bitwise OR of the two.
If condition1 tests false, Phoenix evaluates each successive Elself condition. When Phoenix
finds a true condition the statements immediately following the associated
Then are executed. If none of the Elself conditions are true, the statement blocks
that follow the Else are executed. After executing the statements following
Then or Else, the program continues with the statement following End If.
The Else and Elself clauses are optional. You can have as many Elself clauses
as you need in a block If. All ElseIf clauses must be precede an Else
clause.That is, the Else clause must be the last clause.
A block If statement must be the first statement on a line. Be careful to
include a matching End If statement for each block If.
Standard numeric and string expressions cannot be evaluated in the same
conditional expression as expressions that use the form If TypeOf object Is objecttype. Use a separate Elself block for objects.
For example:
If a > 1 And a <= 100 Then
...
ElseIf TypeOf controlVar Is ListBox Then
...
End If
Note: The Select Case statement may be more useful when testing several possible
conditions for a single expression.
See Also:
Select Case