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.  With this reasoning she still does what she wants, but that’s kids for you. When we’re programming we have to be 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, it has to perform a certain action.   This my friends is the IF Statement and this tutorial will cover 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 virtually every high level programming languages since, including Visual Basic, and shows no signs of disappearing. The beauty of the IF Statement lies in both 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.  Which implies that If she didn’t feed the dog lego then something else would happen (typically with a happier ending).  When programming, we use the IF Statement to do exactly the same.  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.  Rather than executing each line of code sequentially,  the IF statement will branch the code to execute differing blocks 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 (but never both!) is executed. The basic syntax of the IF Statement is

     IF (condition = true e.g. feed the dog lego)
          The condition is true - execute code block
     ELSE
          The condition isn't true - execute this code block
     END IF

The END IF statement lets Visual Basic know that the IF Statement is finished, it closes the block. Lets put this into practise so you can see what I mean Create a new Project called “IF Statement” Add:

  • A new TextBox to the form called 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 Click Event to ButtonCalculate with the following code

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

Run the project, enter your age into the textbox 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? By executing the IF Statement. Lets look at this in more detail. The code is essentially saying <pre> If the number in the textbox is less than 3 Tell the user they are younger than my niece Else Tell the user they are older than my niece End If </pre> In the first line , the CInt is converting the text in the textbox into an Integer.  Once we have an integer we’ll cover this in more detail later

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, ie 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. Replace the button click event with this code

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

Run your project again and what happens when you enter the number 3? The last code block is executed, and you receive a Message Box stating “You are 3, the same age as Chris’ niece”. So what is happening here? We have a new condition,

ElseIf CInt(TextBoxAge.Text) > 3 Then
     MsgBox("You are older than Chris' niece")

This code is executed is executed if the original IF Statement is False. The code block within the ElseIf will be executed if the condition in the ElseIf is true. Else the next condition is executed. The ElseIf statement has the syntax

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 ElseIfs as you want, but they must be preceded by an IF Statement. The Else Statement ought to come last and will execute if all the Ifs and ElseIfs are false. Try adding more conditions to this code block to study the behaviour of this fundamentally important facet of Visual Programming.