Conditional Logic - If Statements

What is conditional logic? Well, it's something you use in your daily life all the time, without realising you're doing it. Suppose that there is a really delicious cream cake in front of you, just begging to be eaten. But you are on a diet. The cake is clearly asking for it. So what do you do, eat the cake and ruin your diet? Or stick to your diet and let somebody else have that delicious treat? You might even be saying this to yourself:

IfI eat the cake Then my diet will be ruined

If I don't eat the cake Then I will be on course for a slimmer figure

Note the words If and Then in the above sentences. You're using conditional logic with those two words: "I will eat the cake on condition that my diet is ruined". Conditional logic is all about that little If word. You can even add Else to it.

If I eat the cake Then my diet will be ruined

Else

If I don't eat the cake Then I will be on course for a slimmer figure

And that is what conditional Logic is all about - saying what happens if one condition is met, and what happens if the condition is not met. Visual Basic uses those same words - If, Then, Else for conditional Logic. Let's try it out.

Start a new project. Give it any name you like. In the design environment, add a Button to the new form. Double click the button and add the following code to it:

Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click

Dim firstname As String

firstname = "Bill"
If firstname = "Bill" Then MessageBox.Show("firstname is Bill")

End Sub

Run the programme and see what happens. You should get a Message Box with the words "firstname is Bill" in it.

What we did was to set up a string variable and put the name "Bill" into it. When then used conditional logic to test what was in the variable. In fact, we used an If statement. We said:

If the variable called firstname holds the value "Bill" Then display a Message Box

We can tidy that up a bit, because a single line of code can get very long with If statements. We can use this format instead.

If firstname = "Bill" Then

MessageBox.Show( "firstname is Bill" )

End If

That's a lot tidier. Note that we start a new line after the word Then.

  1. The first line contains our condition: "If the following condition is met".
  2. The second line is what we want to do if the condition is indeed met.
  3. The third line tells Visual Basic that the If statement ends right here.

Try this. Delete the two quotation marks around the word Bill in the If Statement. Your code should now be this:

Dim firstname as String

firstname = "Bill"

If firstname = Bill Then

MessageBox.Show( "firstname is Bill" )

End If

VB.NET puts a wiggly line under Bill. If you try to start your programme, you'll get a message box telling you that there were Build Errors, and asking if you want to continue.

Say No to return to the design environment. The reason for the blue wiggly line is that VB insists on you using double quotes to surround you text. No double quotes and VB insists it's not a string of text.

Change you code to this.

firstname = "Phil"

If firstname = "Bill" Then

MessageBox.Show("firstname is Bill")

Else

MessageBox.Show("firstname is not Bill")

End If

Now run the programme and see what happens when you click the button.

 

 

You should have gotten a Message Box popping up saying "firstname is not Bill". The reason is that we included the Else word. We're now saying, "If the condition is met Then display one Message Box. If the condition is not met, display a different Message Box." Notice that the Else word is on a line of it's own.

Now, after you have tested your programme, try this. Add a textbox to your form. Then change this line in your code:

firstname = "Phil"

To this:

firstname = Textbox1.Text

What the code does is to transfer the text in the Textbox directly to the firstname variable. We can then test what is in the variable with an If statement.

When you've finished the code, test it out by typing the word "Bill" (with a capital B) into the textbox, and then clicking the button. Then try it with a lower case "b".

 

So far, we've explored only simple If statements, and we're going to leave it that way for now. But they can get quite complex, because you can have one If statement inside another, and multiple Else statements.

The code you have just wrote, however, does demonstrate how you can find out what is in a variable, and take action if the condition is either met or not met. We're now going to explore another way to do that - the Select Case statement.