Wednesday, April 17, 2013

[VB.NET] If-Then 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.

An example would be. If I eat the cake Then my diet will be ruined.

Here is an example of using if-then statements


Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim firstname As String
firstname = "Bill"
If firstname = "Bill" Then MsgBox("firstname is Bill")
End Sub

Run the program 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

No comments:

Post a Comment