Name
If...Then...Else Statement
Syntax
Ifcondition
Then [statements
] [ElseIfcondition-n
Then [elseifstatements
] ... [Else [elsestatements
]] End If
Or, you can use the single line syntax:
Ifcondition
Then [statements
] [Elseelsestatements
]
-
condition
(required; Boolean ) An expression returning either
True
orFalse
or an object type-
statements
(optional) Program code to be executed if
condition
is true-
condition
-n
(optional) Same as condition
-
elseifstatements
(optional) Program code to be executed if the corresponding
condition-n
isTrue
-
elsestatements
(optional) Program code to be executed if the corresponding
condition
orcondition-n
isFalse
Description
Executes a statement or block of statements based on the Boolean
(True
or False
) value of an
expression
Rules at a Glance
If
condition
isTrue
, the statements following theIf
are executed.If
condition
isFalse
and noElse
orElseIf
statement is present, execution continues with the correspondingEnd
If
statement. Ifcondition
isFalse
andElseIf
statements are present, the condition of the nextElseIf
is tested. Ifcondition
isFalse
and anElse
is present, the statements following theElse
are executed.In the block form, each
If
statement must have a correspondingEnd
If
statement.ElseIf
statements do not have their ownEnd
If
. For example:If
condition
Thenstatements
ElseIfcondition
Thenstatements
End IfElseIf
andElse
are optional, and any number ofElseIf
andElse
statements can appear in the block form. However, noElseIf ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.