Share via


Visual Basic.Net: Conditional Branches

 

 

What is a conditional branch?

Conditional branches are how computers can be programmed to make decisions based on varying conditions.

Without conditional branches, computers would not be able to 'Think'. 

How do conditional branches work?

There are 2 kinds of conditional branches in Visual Basic(excluding conditional branches in LINQ(Where))

IF-Then - http://msdn.microsoft.com/en-us/library/752y8abs(v=vs.80).aspx 
The first line of an If-Then statement evaluates an expression that is in-between the If and the Then like this: 

  • If <Expression> Then

  • But one thing many people fail to realize is that the expression in-between the If and the Then is reduced down to a Boolean value, before it is evaluated by the If-Then statement.

    • Example
    • (1 = 2) Will return a Boolean False
    • (1 = 1) Will return a Boolean True
    • (1 + 1 = 3) Will return a Boolean False
    • (1 > 3) Will return a Boolean False
    • (1 < 3) Will return a Boolean True
    • There are more predictable scenarios.
  • One more case to point out as well, an inversion in-between the If and Then will invert the expression, or is otherwise considered to be a part of the expression.

    • (Not 1 = 1) Will return a Boolean False
    • Not, inverts an input on an electronic level(Flip-Flops), and since computers only really understand 1's and 0's, a 1 is equivalent to a Boolean true, while a 0 is equivalent to a Boolean False. Knowing this, we know that the inversion(output of Not) for the number 1(True) is 0(False), and the inversion for the number 0 is the number 1.

    In summary, the first line breaks down to this when being evaluated by If Then:   * If <Boolean> Then*

Now that the first line has come and gone, we go to the next line, but not really the next line, more like the next section. The next section includes however many lines are in-between the first line, and a statement such as "Else" or "ElseIf" or "End If".

  • This section, you can only arrive at if the first line simplified to this: *** If** <True> Then*
  • You will never arrive in this section if the first line simplified to this: *** If** <False> Then*

Now lets say that our first line reduced to this: If <False> Then

  • Since we would never arrive in the first block, we may arrive at a couple different places, depending on how you wrote your conditional branch.
    • We could Arrive at an ElseIf, which is going to work just like the first line of code: ElseIf <Expression>
    • In order to arrive in the next block after that ElseIf, the statement would have to simplify to this: ElseIf <True>
    • So you can guess how this works, right?

You can have as many ElseIf statements as you want, and the rules remain the same. Now lets say that you have gone through your first line, gone through all of your ElseIf's, and finally you end up here, at your last resort "Else". This branch will execute if:

  • 1.) You include it(It's optional)
  • 2.) All other branches failed to evaluate an expression that returned True.
  • That's it!

Finally, you arrive at your "End If" statement, which ends the conditional branch
Example

Option Strict On
Public Class  Form1
 Private Sub  Button1_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button1.Click
 Dim Name As String  = "Paul"
 If Name = "Paul"  Then
  MsgBox("Hello Paul!")
 ElseIf Name = "Jim"  Then
  MsgBox("Hello Jim")
 Else
  MsgBox("Who are you?")
 End If
 End Sub
End Class

Select-Case - http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.71).aspx
 Select Case works much in the same way that If-Then, except its more linear, and therefore neater.

  • Lets say your first line is Select Case 1
  • and your have 4 branches in that statement(Case 1, Case 2, Case 3, Case Else)
  • It's very simple, Select Case creates an expression out of the two parts you have provided
  • The first part you provide is in line 1, after Select Case:   Select Case <Expression>
  • Only this time, select case does not break it down to a Boolean(yet), select case puts it on one side of the equation.
  • 1 = ?
  • Now Select Case goes to each branch, placing the Case expression on the second side of the equation now.
  • If Select Case builds an expression that returns Boolean <True>, then that branch of code is executed.
  • If all other branches fail, and there is a Case Else branch included, then that branch will get executed
  • Otherwise End Select will be reached and the conditional branch will be done.
  • So in summary Select Case builds an expression, and then evaluates it on a case by case basis
  • <expression>=<expression>=Boolean 

Example

Option Strict On
Public Class  Form1
 Private Sub  Button1_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button1.Click
 Dim Name As String  = "Paul"
 Select Case  Name
  Case "Paul"
  MsgBox("Hello Paul!")
  Case "Jim"
  MsgBox("Hello Jim!")
  Case Else
  MsgBox("Who are you?")
 End Select
 End Sub
End Class

Summary

Conditional branches are a fundamental component of programming and computers.

Please check out my other Technet Wiki articles!