Compartilhar via


If...Then...Else Statement

 

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

Syntax

' Block syntax: 
If condition Then
   [statements]
[ElseIf condition-n Then
   [elseifstatements]] . . .
[Else
   [elsestatements]]
End If 

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

Arguments

  • condition
    One or more of the following two types of expressions:

    A numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as False.

    An expression of the form TypeOf objectname Is objecttype. The objectname is any object reference and objecttype is any valid object type. The expression is True if objectname is of the object type specified by objecttype; otherwise it is False.

  • statements
    One or more statements separated by colons; executed if condition is True.

  • condition-n
    Same as condition.

  • Elseifstatements
    One or more statements executed if the associated condition-n is True.

  • Elsestatements
    One or more statements executed if no previous condition or condition-n expression is True.

Remarks

Block Syntax

When a block If is executed, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf (if any) is evaluated in turn. When a True condition is found, the statements following the associated ElseIf are executed. If none of the ElseIf statements are True (or there are no ElseIf clauses), the statements following Else are executed. After executing the statements following Then or Else, execution continues with the statement following End If.

The Else and ElseIf clauses are both optional. You can have as many ElseIf statements as you want in a block If, but none can appear after the Else clause. Block If statements can be nested; that is, contained within one another.

A block If statement must be the first statement on a line. The block If must end with an End If statement.

Single-Line Syntax

You can use the single-line syntax for short, simple tests. However, the block syntax provides more structure and flexibility than the single-line form and is usually 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.

When you use the single-line syntax, you can execute multiple statements as the result of an If...Then decision, but they must all be on the same line and separated by colons.

The following example illustrates the use of the block syntax.

dim count
count = 0

If count = 0 Then
    MsgBox "There are no items."
ElseIf count = 1 Then
    MsgBox "There is 1 item."
Else
    MsgBox "There are " & count & " items."
End If

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

If A > 10 Then A = A + 1 : B = B + A : C = C + B

Requirements

Version 1

Change History

Date

History

Reason

April 2009

Reorganized "Remarks" section.

Information enhancement.

April 2009

Added a second example.

Customer feedback.

See Also

Using Conditional Statements
Operators (VBScript)