If...Then...Else Statement

   
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 statements can appear after an Else .

  • condition can be any statement that evaluates to True or False .

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

  • You can also use the If statement to determine object types by using the TypeOf and Is keywords, as follows :

     If TypeOf   objectname   Is   objecttype   Then 
  • statements are only optional in the block form of If . However, statements are required when using the single-line form of If in which there is no Else clause.

Programming Tips and Gotchas

  • You can use the single-line form of the If statement to execute multiple statements, which you can specify by delimiting the statements using colons. However, single-line If statements are hard to read and maintain and should be avoided for all but the simplest of situations.

  • In situations where you have many possible values to test, you will find the Select Case statement much more flexible, manageable, and readable than a bunch of nested If statements.

  • You will come across situations in which very large blocks of code have to be executed based one or more conditions. In these and in all situations you should try to make your code as readable as possible, not only for other programmers, but for yourself, since you will probably need to revisit the code several months down the line. For example, consider a scenario in which, at the beginning of a procedure, a check is made to see if the procedure should be executed under a given set of circumstances. You have the choice of surrounding the whole code with an If...Then...End If construct, like this:

     If iSuccess Then     ...     ...     ... End If 

    Or you can instead check for a False condition and, if found, exit the subroutine:

     If Not iSuccess Then     Exit Sub End If ... ... ... 

    The latter alternative can be much easier to read.

  • Indentation is important for the readability of If , and especially nested If , statements. The set of statements within each new If...Else...EndIf block should be indented. When using the Visual Studio IDE, you can simply select a block of code and press the tab key to indent the complete selected block. The following example shows correctly indented code:

     If x = y Then     DoSomethingHere     If y < z Then         DoSomethingElseToo     Else         DoAnotherThing         If z - 1 = 100 Then             DoAThing         End If     End If Else     DoAlternative End If 
  • You may often run into code such as:

     If iSuccess Then ... 

    where iSuccess is an Integer variable. The statement works because Visual Basic interprets all non-zero values as equal to Boolean True and all zero values as equal to Boolean False . However, if Option Strict is on, statements such as these will generate a compiler error, since VB.NET will not automatically convert the iSuccess integer to the Boolean required by the If statement.

  • 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. The most common of these is And and Or . You can create conditions like:

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

    Note the use of parentheses to improve readability.

  • VB.NET has introduced the AndAlso and OrElse operators, which work exactly like the And and Or operators, respectively, except that they evaluate the statement parts from left to right only until enough information is obtained to determine the truth value of the whole statement. For example, consider the statement

     If (X AndAlso Y) Then 

    If X is False , then Y is not evaluated because the entire statement is False regardless of the truth value of Y. This is referred to as short-circuiting . It provides a significant advantage in case evaluation of Y would produce an error. For example, we want to employ short-circuiting in the following case

     If (x <> 0) AndAlso (1/x > 10) Then . . . 

    because in this case if x = 0 , then the statement 1/x>10 will produce an error if it is evaluated.

  • The If statement is also used with objects to determine if an object variable is Nothing . This is done using the Is operator:

     If Not (   objectvar   Is Nothing) Then 

See Also

IIf Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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