If...then...else Statement
Conditionally executes a group
ofstatements, depending on the value of
anexpression.
Syntax
·
Single line
syntax
if condition then
statement
·
Block form
syntax
if condition [then]
[statement]
[else
elsestatements]
endif
·
Block form
syntaxusing elseif
if condition [then]
[statement]
[elseif
elsestatements]
[elseif
elsestatements]
[elseif
elsestatements]
endif
The if...then...else statement
syntax has these parts:
|
Part
|
Description
|
|
condition
|
Required.
Anumeric
expression orstring
expression that evaluates to true or false.
|
|
statement
|
Optional in block form; required in
single-line form that has no else clause.
|
|
elsestatements
|
Optional. One or more statements executed
if no previous condition expression is true.
|
Remarks
You can use the single-line form (first
syntax) for short, simple tests. However, the block form (second
syntax) provides more structure and flexibility than the
single-line form and is usually easier to read, maintain, and
debug.Using elseif keyword allows you to avoid having
multi-level if...endif statements when you need
switch-like logic.
Note: With the single-line
form, it is possible to have multiple statements executed as the
result of an if...then decision. All statements must be on
the same line and separated by colons, as in the following
statement:
inta
=
11
ifa>10
then
a=a+1
Println(a)
A
block form If statement must be the first statement on a
line. The else and endif parts of the statement can
have only aline
number orline
labelpreceding them. The block if must
end with an endif statement.
To
determine whether or not a statement is a block if, examine
what follows the then keyword. If anything other than
acomment
appears after
then on the same line, the statement is treated as a
single-line if statement.
The else is optional. Block
if statements can be nested; that is, contained within one
another.
|