O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

If...Then...Else Statement

Syntax

If condition Then
    [statements]
[ElseIf condition-n Then
    [elseifstatements] ...
[Else
    [elsestatements]]
End If

Or, you can use the single line syntax:

If condition Then [statements] [Else elsestatements]
condition (required; Boolean )

An expression returning either True or False 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 is True

elsestatements (optional)

Program code to be executed if the corresponding condition or condition-n is False

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 is True, the statements following the If are executed.

  • If condition is False and no Else or ElseIf statement is present, execution continues with the corresponding End If statement. If condition is False and ElseIf statements are present, the condition of the next ElseIf is tested. If condition is False and an Else is present, the statements following the Else are executed.

  • In the block form, each If statement must have a corresponding End If statement. ElseIf statements do not have their own End If. For example:

    If condition Then
       statements
    ElseIf condition Then
       statements
    End If
  • ElseIf and Else are optional, and any number of ElseIf and Else statements can appear in the block form. However, no ElseIf ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required