Skip to content

Latest commit

 

History

History
132 lines (93 loc) · 6.15 KB

if-then-else-statement.md

File metadata and controls

132 lines (93 loc) · 6.15 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: If...Then...Else Statement (Visual Basic)
If...Then...Else Statement
04/16/2018
vb.ElseIf
vb.Then
vb.If
vb.EndIf
ElseIf statement [Visual Basic], If...Then...Else
Then statement [Visual Basic], If...Then...Else
control flow [Visual Basic], branching
execution [Visual Basic], conditional
TypeOf...Is expression, and If...Then...Else statements
If...Then...Else statements
If statement [Visual Basic], If...Then...Else
If statement [Visual Basic]
Is operator [Visual Basic], in If...Then...Else statements
If Operator [Visual Basic]
branching [Visual Basic], conditional
If function [Visual Basic], and If...Then...Else statements
Else statement [Visual Basic]
790068a2-1307-4e28-8a72-be5ebda099e9

If...Then...Else Statement (Visual Basic)

Conditionally executes a group of statements, depending on the value of an expression.

Syntax

' Multiline syntax:
If condition [ Then ]
    [ statements ]
[ ElseIf elseifcondition [ Then ]
    [ elseifstatements ] ]
[ Else
    [ elsestatements ] ]
End If

' Single-line syntax:
If condition Then [ statements ] [ Else [ elsestatements ] ]

Quick links to example code

This article includes several examples that illustrate uses of the If...Then...Else statement:

Parts

condition
Required. Expression. Must evaluate to True or False, or to a data type that is implicitly convertible to Boolean.

If the expression is a Nullable Boolean variable that evaluates to Nothing, the condition is treated as if the expression is False, and the ElseIf blocks are evaluated if they exist, or the Else block is executed if it exists.

Then
Required in the single-line syntax; optional in the multiline syntax.

statements
Optional. One or more statements following If...Then that are executed if condition evaluates to True.

elseifcondition
Required if ElseIf is present. Expression. Must evaluate to True or False, or to a data type that is implicitly convertible to Boolean.

elseifstatements
Optional. One or more statements following ElseIf...Then that are executed if elseifcondition evaluates to True.

elsestatements
Optional. One or more statements that are executed if no previous condition or elseifcondition expression evaluates to True.

End If
Terminates the multiline version of If...Then...Else block.

Remarks

Multiline syntax

When an If...Then...Else statement is encountered, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf statement (if there are any) is evaluated in order. When a True elseifcondition is found, the statements immediately following the associated ElseIf are executed. If no elseifcondition evaluates to True, or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then, ElseIf, or Else, execution continues with the statement following End If.

The ElseIf and Else clauses are both optional. You can have as many ElseIf clauses as you want in an If...Then...Else statement, but no ElseIf clause can appear after an Else clause. If...Then...Else statements can be nested within each other.

In the multiline syntax, the If statement must be the only statement on the first line. The ElseIf, Else, and End If statements can be preceded only by a line label. The If...Then...Else block must end with an End If statement.

Tip

The Select...Case Statement might be more useful when you evaluate a single expression that has several possible values.

Single-Line syntax

You can use the single-line syntax for a single condition with code to execute if it's true. However, the multiple-line syntax provides more structure and flexibility and is easier to read, maintain, and debug.

What follows the Then keyword is examined to determine whether a statement is a single-line If. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement. If Then is absent, it must be the start of a multiple-line If...Then...Else.

In the single-line syntax, you can have multiple statements executed as the result of an If...Then decision. All statements must be on the same line and be separated by colons.

Multiline syntax example

The following example illustrates the use of the multiline syntax of the If...Then...Else statement.

[!code-vbVbVbalrStatements#101]

Nested syntax example

The following example contains nested If...Then...Else statements.

[!code-vbVbVbalrStatements#102]

Single-Line syntax example

The following example illustrates the use of the single-line syntax.

[!code-vbVbVbalrStatements#103]

See also