The IF Statement in Visual Basic

I have a couple of nieces.   One is 3 years old and the other 7.   The 3 year old is a wonderfully single minded character and we have to spell out the consequences of any action to her.

“If you feed the dog lego (again) then the dog will feel poorly (again)”.  

“If you put makeup on the wall then mummy gets very upset”.  

“If you don’t eat your food then you can’t eat the ice cream after”.  

You get the picture.  Without this level of reasoning she does what she wants.  (As an aside, even with this reasoning she still does what she wants, but that’s kids for you). When we’re programming we have to be just as precise with our instructions.  Unlike my niece, the computer will do exactly what we tell it; frustratingly so at times.  And often we need to tell the computer that if a certain condition is true then it has to perform a certain action.   This, my friends, is the IF Statement and this tutorial will cover how to use the IF Statement in Visual Basic.

In 1952, Stephen Kleene introduced the concept of the IF Statement to the world in his elaborately titled book, Introduction to Metamathematics.  Little did he know that his book would go on to influence the life virtually every programmer. The beauty of the IF Statement lies in its simplicity and its power, a potent combination which best explains why it is still so ubiquitous 60 years later.   In the case of my niece, we told her that if she did or didn’t do something there would be a consequence.  If she fed the dog lego then there would be a consequence.  The statement also implies that if she didn’t feed the dog lego then something else would happen.  When programming, we use the IF Statement to do exactly the same.

The IF Statement instructs Visual Basic that if a condition is true, execute some code.  Optionally we can say if the condition isn’t true, execute some other code.

The statement is perhaps the most commonly used means of controlling the flow of code execution in your application.  Rather than executing each line of code sequentially, line by line, the IF statement will branch the code.  We can instruct Visual Basic to execute blocks of code based on conditions, as represented in the diagram below.
IF Statement

A condition, in this case the IF Statement, is executed and depending on the results of that statement, subsequent statements, either y or n is executed. But never both!  The basic syntax of the IF Statement is:

     IF (condition = true)
          The condition is true - execute code block
     ELSE
          The condition isn't true - execute this code block
     END IF

The END IF statement at the end there lets Visual Basic know that the IF Statement is finished.   It closes the block.   Let’s put this into practise so you can see what I mean.

Create a new Project called “IF Statement” Add:

  • A new TextBox to Form1 called TextBoxAge.
  • A Label with the Text “Enter your age”.
  • A Button with the Name ButtonCalculate and the Text “Younger or Older”.

Style the form so it looks as follows:

enter-age

Add the following code to the Click Event of ButtonCalculate:

If CInt(TextBoxAge.Text) < 3 Then
     MessageBox.Show("You are younger than Chris' niece")
Else
     MessageBox.Show("You are older than Chris' niece")
End If

Run the project, enter your age into the textbox, click the Younger or Older button, and what do you see?  The Message Box will likely, unless you are some sort or prodigy, state that you are older than my niece.  How does it know?  It “knows” by executing the code in the IF Statement. Let’s look at this in more detail. The code is essentially saying:

  • If the number in the textbox is less than 3 then tell the user they are younger than my niece
  • Else tell the user they are older than my niece
  • End If

Take a look at the first line of code again.  The statement CInt(TextBoxAge.Text) is converting the text in the textbox to an Integer.

If CInt(TextBoxAge.Text) < 3 Then

If this resulting number is less than three then the MsgBox statement pops up the Message Box with the string you define.  In this case, “You are younger than Chris’ niece”.

If this condition isn’t true, i.e. you are the same age as or older than my niece, the second block is executed, a messagebox telling the user that they are older than my niece.

This works well unless you are 3 years old.  When the Visual Basic application incorrectly states that you are older than my niece.

To correct this, replace the button click event with this code:

If CInt(TextBoxAge.Text) < 3 Then
     MessageBox.Show("You are younger than Chris' niece")
Else If CInt(TextBoxAge.Text) < 3 Then
     MessageBox.Show("You are older than Chris' niece")
Else
     MessageBox.Show("You are 3, the same age as Chris' niece")
End If

Run your project again and enter the number 3.  What happens?  You see a message stating that you are the same age as my niece.  The last block of code is executed in the IF Statement.

So what is happening here?  We actually have three conditions to check in the IF block, the code is branched 3 ways.

  • If your age is less than 3 then a block is executed.
  • Else, if your age is greater than three then another block is executed.
  • Else, your age is neither greater than nor less than three, execute the last block.

This flow of logic is represented below:

IF (condition = true)
     The condition is true - execute code block
ELSEIF (another condition = true)
     The previous condition was false, this condition is true - execute this code block
ELSE 
     Neither conditions are true, execute this code block
END IF

You can add as many Else Ifs as you want in an IF ELSE IF ELSE block.  These Else Ifs must be preceded by an If statement.  The Else Statement is optional but must come last.

Try adding more conditions to this code block to really get to grips with what is happening here.

You now understand in principle one of the most fundamental building blocks of Visual Basic programming.

Now look at the video of this tutorial!