If Then Else Statement

If...Then statement is used to run code based on conditions. Programs need to do different things in response to different conditions The If...Then statement allows you to evaluate a condition and to then run different sections of code based on the results of that condition.1 For example, you might want your program to check a user's age before outputting the ticket price for a charity concert.

Syntax

If condition Then
[ statements ]
ElseIf condition Then
[ statements ]
ElseIf condition Then
[ statements ]
.....
.....
Else
[ statements ]
End If

The following example demonstrates how the If...Then statement works.

Dim Age As Integer
Age = InputBox("Enter Your AGE: ", "Ticket Price")
If age > 65 Then
MessageBox.Show("Seniors Discount! You Pay $16.00")
ElseIf Age < 12 Then
MessageBox.Show("Children's Price! You Pay $10.00")
Else
MessageBox.Show("Full Price! You Pay $18.00")
End If

Exercise

1. Write a program that asks the user to enter the plural of the word SHEEP. If the answer is SHEEP, a congratulatory message should be displayed.

2. Create a program that asks for a number and, depending on the value entered, displays POSITIVE, NEGATIVE, or ZERO.

3. Write a program that asks for a number, a second number, and then the sum of the two numbers. If the correct sum is entered, a congratulatory comment should be displayed. If the answer is incorrect, the program should indicate that the user made an error.

4. Write a program which displays a different message depending on the age given. Here are the possible responses.

  • age is less than 16, say "You can't drive."
  • age is less than 19, say "You can't drink."
  • age is less than 21, say "You can't rent a car."
  • age is 25 or over, say "You can do anything that's legal."

5. Julio Cesar Ali VII is an interplanetary space boxer, who currently holds the championship belts for various weight categories on many different planets within our solar system. However, it is often difficult for him to recall what his "target weight" needs to be on earth in order to make the weight class on other planets. Write a program to help him keep track of this. The program should input his earth weight, and the planet he wants to fight on. It should then compute his weight on the destination planet based on the table below:

You can either input the planet by its number or challenge yourself to use radio buttons to input the planet name.

# Planet Relative Gravity
1 Venus 0.78
2 Mars 0.39
3 Jupiter 2.65
4 Saturn 1.17
5 Uranus 1.05
6 Neptune 1.23

6. Mike Loudman, Jane Quandry and Herbert Quinn are running for election to parliament. Write a program to input the number of votes cast for each candidate and compute the standings in order from highest to lowest.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.