Publishing System Settings Logout Login Register
Visual Basic .NET/VB6 – If Statements and Select Case
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on October 16th, 2005
18711 views
Visual Basic
Visual Basic .NET/VB6 � If Statements and Select Case
Most of this tutorial should work with VB6, but I�m not sure.

What�s a selection you ask?

Selection is basically one/many lines of code that may/may not be executed depending on whether the condition set is true or false.

Different types of selection:

1. IF Statement (This has three variations explained below)
2. Select Case (Will be explained in a later tutorial)

The IF statement as said above has three variations

1. If Then
2. If Then�Else
3. If Then�ElseIF�If Then�Else If

If statements are Booleans, which means true or false. Depending on the code, it will go through a line to lines of code that are true or false and on reaction of finding the true/false it will act how it�s coded too.



If Then:

If Then is usually consists of one true/false statement. Lets make an easy one, open up either Visual Basic 6 or Visual Basic .NET and create a new application and call it whatever you want. Add one label, one button, one text box and give it them appropriate names, these are what I used; lblOutput, btnCheckAge, txtAge.

Now double click on your button and put the code in below and if you�ve used a different name for you text box then modify the code for it:

Dim vAge as Integer
vAge = txtAge.Text
If vAge > 17 Then �Starts the IF statement and checks if the user is over 17 or not (True = 17 or older or False = 16 and less)
MsgBox(�You are old enough to drive within the UK�)
End IF


The picture below shows the code on in my program:

If Then code for the program



Code break down:

Dim vAge as Integer � This is just declaring your variable, which is temporary storage
vAge = txtAge.Text � This is assigning the contents within txtAge to the variable vAge
If vAge > 17 Then � Checks if the age entered into the text box is smaller or greater than 17. This is done with the �>� this means greater than, and �<� means smaller than and �=� means equals too.
MsgBox(�You are old enough to drive within the UK�) � This displays the message box if you enter an age over 17.
End IF � THIS IS IMPORTANT!! No IF statement will work without this. This tells the program where the IF statement ends.

Now press the run button and if all has been done right it should show the same as below:

This is the IF then program working

Now there�s a problem! What about if the age entered is younger than 17 or equal to 17? This brings us to If Then�Else

If Then...Else

If Then�Else deals with the False condition. Now open up the project we did for the If Then statement and lets modify it to for the If Then�Else statement.

Where it says If vAge > 17 add an equals sign after the greater sign (>) so it looks like this If vAge >= 17.
Then just before the End IF add:
Else �This tells the program that if the condition is false it will execute the code that�s written below.
Msgbox(�Sorry your too young to drive!�)

Your code should now look like below:

the code for the If Then...Else form

Now run the program and enter ages above, equal to and lower than 17, and it should come up with the appropriate message boxes.



If Then�ElseIF

Now, we�ve used the If Then & If Then�Else, but what about if there�s more than two endings to an IF Statement? We use If Then�ElseIF. This allows us to add more conditions to our IF statement.

Continuing with our program from that past two examples, lets modify the code to use the If Then�ElseIf instead of the �=� sign to help identify if the user is the same age as 17.

Open up your program and then double click on your button. Change all the code like I�ve done below:

Dim vAge As Integer
vAge = txtAge.Text
If vAge > 17 Then 'Checking if the user is over 17 or not (True = 17 or older or False = 16 and less)
MsgBox(\"You are old enough to drive within the UK\")
ElseIf vAge = 17 Then 'Tells the program that if above statement isn't true, this could be true, giving the program another alternative
MsgBox(\"You are legal, but you are only 17\")
Else
MsgBox(\"Sorry your too young to drive!\")
End If


Your code area will now look like this:

If then...elseIF code

Run the program and it should handle all ages entered (within reason)

Now that can be confusing so heres a break down of the code:

Dim vAge As Integer - Declares the Variable vAge as an Integer
vAge = txtAge.Text - Assigns the text in txtAge to the variable
If vAge > 17 Then � Checks if the variable is greater than 17 and if it is it executes the next line of code, if not it skips to the elseIf
MsgBox("You are old enough to drive within the UK")
ElseIf vAge = 17 Then � Checks if the variable is equal to 17 if it is it executes the next line of code, if not it skips finally to the FALSE condition which is Else.
MsgBox("You are legal, but you are only 17")
Else � It says rest of the code was false so now I must execute the code below.
MsgBox("Sorry your too young to drive!")
End If

ElseIf is like the first part of AN If statement as it�s still undecided if the data entered is true or false, so any If Then�ElseIf will always be undecided up until the condition hit�s true of false.

Note: Else ALWAYS means the rest of the code turned out False.



If Then�Else If

Now the code above can be written over more lines instead of one, but it will still have the same output. Open up the program and modify the code like so:

Dim vAge As Integer
vAge = txtAge.Text
If vAge > 17 Then 'Checking if the user is over 17 or not (True = 17 or older or False = 16 and less)
MsgBox(\"You are old enough to drive within the UK\")
Else
If vAge = 17 Then 'Tells the program that if above statement isn't true, this could be true, giving the program another alternative
MsgBox(\"You are legal, but you are only 17\")
Else
MsgBox(\"Sorry your too young to drive!\")
End If 'End of If > 17
End If 'End of If = 17
End Sub


Your code should now look like below:

If then else if part 2

Now as you can see it has two End If right at the end, this is because there are actually two If statements within the If statement. This is because where the ElseIf should be, the Else has its own line and the If vAge = 17 is on a different line, so therefore we need another matching End If. When you run the program it should do exactly the same as the others.

Now, we�ve covered basic IF Statements, but what if TWO conditions have to be true? Well then we have to use the �AND� Condition.

The �AND� Condition

An AND condition is used when an IF statement requires to statements and both of these statements must be true. If one of the statements is false, the whole Boolean becomes false. Open up the previous project and added another text box to the form, then name it txtGender then double click the button and enter this code:

Dim vAge As Integer
Dim vGender As String
vAge = txtAge.Text
vGender = txtGender.Text
If (vAge >= 18) And (vGender = \"female\") Then 'Checks if both variables (vAge & vGender) equal and female if not the boolean will return false.
MsgBox(\"You are old enough to enter the nightclub\")
Else 'Tells the program that code will be executed on the FALSE boolean
MsgBox(\"Sorry your too young to enter\")
End If


Your code on your Code Form should now look like this:

And IF code



Now to understand the code, here is a breakdown of it:
Dim vAge As Integer � Declares the variable vAge as integer
Dim vGender As String � Declares vGender as string
vAge = txtAge.Text � Assigns the text in txtAge to to the variable vAge
vGender = txtGender.Text � Assigns the text in txtGender to to the variable vGender
If (vAge >= 18) And (vGender = "female") Then � Sets the AND if statement. It checks if the variable vAge is equal to or greater than 18 AND if the person is female, for the next line of code to be executed and the person allowed in the club.
MsgBox("You are old enough to enter the nightclub")
Else 'Tells the program that either one of both of the above conditions were false and executes the next line of code.
MsgBox("Sorry your too young to enter")
End If

Now once all the code has been added run the program and enter �18� then �female� and you should get this:

Program = True

Then if that works, change the age to something below 18 or change the gender to male and you should get this:

program = false

The Or Condition

So now what if there are two possible outcomes yet only one can be true? We have to use an OR condition.

Football teams get an award if they score at least 50 points on 10 or more occasions or they score 35 points on 15 or more occasions, this would be done like below:

Dim Fifty as Integer �VB.NET doesn�t allow numbers as identified so text is chosen
Dim ThirtyFive as Integer
�Assume data has been inputted and saved in the variables
If (Fifty >= 10) or (ThirtyFive >= 35) Then �If either of these are true, they will get an award.
MsgBox(�They get an award�)
End If


Ok here�s a code break down:

Dim Fifty as Integer �Declares the variable Fifty as an Integer
Dim ThirtyFive as Integer �Declares the variable ThirtyFive as an Integer
If (Fifty >= 10) or (ThirtyFive >= 35) Then � Sets the OR condition, for the whole condition to be false both of these statements must return false, for example if 20 was entered in both of the variables they wouldn�t get an award.
MsgBox(�They get an award�)
End If

I would create a project for the OR statement, but it would be going too advanced for this tutorial. In a later tutorial I will go into OR statements in more depth.



Nested If Statements

Why would we use Nested If statements? We would use then as an alternative to writing multiple AND conditions, but this is down to the programmers preference.

From the earlier �AND� program, we can modify these to be a nested IF statement. Open up the program if closed, then double click on the button and put this code in the button:

Dim vAge As Integer
Dim vGender As String
vAge = txtAge.Text
vGender = txtGender.Text
If vAge >= 18 Then 'Checks if the variabloe vAge is equal to or greater than 18 then goes to the next line of code
If vGender = \"female\" Then 'Checks to see if the variable vGender equals female if both are true it executes next line of code if not the condition is FALSE
MsgBox(\"You are old enough to enter the nightclub\")
Else 'Tells the program that code will be executed on the FALSE boolean
MsgBox(\"Sorry your too young to enter\")
End If 'Two End If's because we have to IF's at the beginning of the statement.
End If


Your code should now look like this:

Nested IF

Now run the program and all should run, if not check the code and if all else fails message me I�m glad to help. Heres an explanation of the code:

Dim vAge As Integer � Declares the variable vAge as integer
Dim vGender As String � Declares the variable vGender as string
vAge = txtAge.Text � Assgins the text in txtAge to the variable vAge
vGender = txtGender.Text � Assgins the text in txtGender to the variable vGender
If vAge >= 18 Then � Finds out if the age entered is equal to or greater than 18 if it is then it goes onto the next line of code
If vGender = "female" Then � After finding the last statement is true, the program checks if this is true and if it is, the next line of code is executed, if not it skips it and goes straight to the else.
MsgBox("You are old enough to enter the nightclub")
Else 'Tells the program that code will be executed on the FALSE boolean
MsgBox("Sorry your too young to enter")
End If � Two matching End If�s because of there are two If�s.
End If

Now I hope this tutorial was easy to follow. I did try to explain as best as I could but when I next re-read it, I�ll make modifications. If you can spot any errors message me or post on the forum and if you need any help, message me or post here, and if your adding me to msn make sure you PM me first!!!!

Everything i did in the project has been put on in one program and it's all indavidual meaning that it i have all the stages on different forms. It can be run and looked at that way because it's full coded with a main form.

Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
ronson

I'm James, i'm 19 and i love both software and hardware side of computing. I'm currently
studying A-Level Computing and I-Pro level 3 which is a Technician Course. Message me for
help on P2L my name is ronson ;)
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top