Visual Basic .NET/Branch statements

From Wikibooks, open books for an open world
Jump to: navigation, search

A branch statement, or conditional statement, is code that allows you to test whether statements are true or false and then execute some code based on these comparisons.

The Branch Statements[edit]

  1. If/Else/Endif
  2. If/ElseIf/Else/Endif
  3. Select Case/End Select

The Branch Statements Explained[edit]

If...Else...EndIf Statement[edit]

Refer to these lines for the examples below:

    Dim iJunk1 as integer
    Dim iJunk2 as integer
    iJunk1 = 4
    iJunk2 = 7

The actual format of the IF statement is:

    IF...THEN...ELSE...ENDIF

The IF statement ALWAYS tests the left condition or statement for a TRUE result:

   ie: is iJunk1 = 4 ........ TRUE
   ie: is iJunk1 = 5 ........ FALSE
   ie: is iJunk1 > 7 ........ TRUE
   ie: is iJunk1 < iJunk2 ... TRUE

If the condition or statement is true, the IF statement continues to process the top or next portion of the statement using the THEN operator.

If the THEN operation is a single statement, it can be written on the same line as the test condition:

    IF iJunk1 = 4 THEN iJunk2 = 10

If the THEN operation requires two or more statements you will find that the entire IF statement will be MUCH easier to read when written with each statement on its own line:

    IF iJunk1 = 4 THEN
       iJunk1 = iJunk1 + 3    'iJunk1 would then = 7
       iJunk2 = iJunk2 + 1    'iJunk2 would then = 8
    ENDIF

As shown above, the IF statement does not require and can be used without the ELSE clause.

When your IF statement needs to process both a True and a False condition the use of the ELSE clause is required:

    IF iJunk = 4 THEN 
       (execute the True code) 
    ELSE 
       (execute the False code) 
    ENDIF
 
   IF  NOT iJunk1 = 4 THEN          ' Be carefull of the use of the NOT clause
       (execute the True code)      'iJunk1 is NOT = 4
    ELSE 
       (execute the False code)     'iJunk1 is = 4
    ENDIF

It is sometimes easier to read the condition or statement when you place it within paren's:

    IF  NOT (iJunk1 = 4) THEN ............

If...ElseIf...Else...EndIf Statement[edit]

If/ElseIf statement is used to conditionally execute code based on more than one test condition or statement. If the condition provided in the If statement evaluates to true, the code in the block is executed. Otherwise, execution would proceed to the ElseIf condition or statement, or on to the Else or EndIf statement.

The ElseIf and Else clauses are not required parts of an If statement.

An example of the If/ElseIf/Else branch statement is:

 'The following variable declarations are for the following example only. They are not needed in real life.
 Dim x As Integer
 Dim y As Integer
 
 If x = y Then
   'Whatever will happen if x = y
 ElseIf x < y Then
   'Whatever will happen if x < y
 Else
   'Whatever will happen if x isn't = to y and x isn't < to y
 End If

Select Case Statement[edit]

Either strings or numbers can be used for a Select Case statement.

Select Case statements are usually used to avoid long chains of If/ElseIf/.../ElseIf/Else statements.

An example of a Select

 Dim nCPU as Integer
 Select Case nCPU
     Case 0
         'No CPU!
     Case 1
         'Single CPU
     Case 2
         'Dual CPU machine
     Case 4
         'Quad CPU machine
     Case 3, 5 To 8
         '3, 5, 6, 7, 8 CPU's
     Case Else
         'Something more than 8
 End Select

Boolean Operators[edit]

Boolean operators in Visual Basic .NET now accommodate for short circuit boolean evaluation, most other languages always apply short circuit boolean evaluation by default (usually, this can be turned off with a compiler option), consider the following boolean statement:

 functionA() and functionB()

With this statement, when short-circuit boolean evaluation is used, the second function will only be called if the first function returns true, this is because if functionA returns false it becomes irrelevant to the outcome of the statement whether functionB returns true or not.

However, when no short-circuit boolean evaluation is used, both of the functions will be called irrespective of whether the first part of the statement returns true or false.

Something to note with short-circuit boolean evaluation is that the order of the parameters can become important when it is used.

Due to previous versions of Visual Basic not having short circuit boolean evaluation, Microsoft has decided to preserve backward compatibility and add two new boolean logic identifiers which support short-circuit boolean evaluation, so in addition to the standard boolean operators:

 Not
 And
 Or
 Xor

There are also two new operators which function using short-circuit boolean evaluation, and they are:

 AndAlso
 OrElse