If...Then...Else Statement


If...Then...Else Statement

Syntax

     If condition [Then]         [statements]     [ElseIf condition [Then]         [statements]] ...     [Else         [statements]]     End If 

Or:

     If condition Then [statements] [Else [statements]] 


condition (required; Boolean)

An expression to be evaluated. If true, the related statements section executes.


statements (optional)

Program code to be executed if condition is TRue. The statements in the Else section only execute if no other section does. In the single-line syntax, multiple statements may be separated by colons.

Description

Executes a statement or block of statements based on the Boolean (TRue or False) value of an expression. If a given condition is TRue, the statements following that condition are executed. If no condition evaluates to TRue, the statements following the Else statement are executed.

Usage at a Glance

  • Any number of ElseIf clauses and related statements blocks may appear. In some cases, a Select Case statement may be a better alternative than numerous End If statements.

  • In the block form, one End If clause ends the entire statement. ElseIf clauses do not have their own End If.

  • The ElseIf and Else blocks are optional. If both are used, the Else block must appear last.

  • If condition returns Null, it will be treated as False.

  • condition can use the Is and TypeOf operators to test for object type, as follows:

         If (TypeOf objectName Is objectType) Then 

  • statements are required when using the single-line form of If in which there is no Else clause.

  • Indentation is important for the readability of If blocks, especially in nested If statements. The set of statements within each new If...Else...EndIf block should be indented, and it is automatically indented by the Visual Studio IDE.

  • It is permissible to write a statement such as:

         If someValue Then ... 

    where someValue is an Integer variable. The statement works because Visual Basic interprets all nonzero values as equal to the Boolean TRue and all zero values as False. However, if Option Strict is On, statements such as these will generate a compiler error. One of the following two statements will restore the condition to its Boolean form.

         If (someValue <> 0) Then 

    or:

         If CBool(someValue) Then 

  • Logical comparison operators can be included in the condition expression, allowing you to make decisions based on the outcome of more than one individual element. For instance, using the Or operator, you can create conditions like:

         If (x = 0) Or (x = 2) Then 

    Parentheses may be included to improve readability and to enforce a specific expression analysis order.

See Also

IIf Function, Select Case Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net