If statements are the primary way that we can change the flow of a program while it is running. It represents a way to make decisions about what to do next. Below is a video of Bill Gates talking about what if statements are and why they are important. He is more eloquent about the topic than I am, and the video has nice graphics included.
We can use if statements to make decisions about anything we can calculate as True or False.
If statements come in a few different forms, each of which has some set of abilities and limitations.
The first form of an if statement is a basic true or false question: We compare two values against one another in such a way that we get a true or false answer. For example, if we have a variable x, we could check to see if x is greater than 0. This form typically looks like this:
if (CONDITION) then
ACTION
end if
The CONDITION is a logical expression that is evaluated to true or false. In the above example our condition would be x > 0.
The ACTION is the set of code instructions that is executed whenever the CONDITION is true. If the CONDITION is false, the ACTION is skipped.
The end if portion tells the computer where the ACTION portion ends.
Now that we understand the general idea of an if statement, we can consider exactly how an if statement is constructed in Python.
if (CONDITION):
ACTION
Notice a few differences between our earlier description: There is no "then," nor is there an "end if." In Python, the "then" is replaced by a colon (:) at the end of the line.
The "end if" requires a little more explanation. Notice that the ACTION is indented (offset from the left). This is how Python knows which commands are to be executed when the CONDITION is true.
if (x > 0):
print("x is positive.")
print("This line only prints when x > 0")
print("This line prints every time. No. Matter. What.")
In the example above, the first 2 print statements are executed ONLY when the CONDITION is true. The 3rd print statement prints every time, regardless of the outcome of the CONDITION.
This form of the if statement is similar to form 1, but with one major difference. In form 1, we use a single CONDITION to decide whether or not to perform an ACTION. In this form, we use a condition to decide which of 2 ACTIONS to perform:
if (CONDITION) then
ACTION 1
else
ACTION 2
end if
When the CONDITION is true, ACTION 1 is executed. When the CONDITION is false, ACTION 2 is executed. In either case one of the actions is skipped.
if (CONDITION):
ACTION 1
else:
ACTION 2
Sometimes, you need to have multiple actions, based on multiple conditions. This is achieved through a If-Else-If statement:
if (CONDITION 1) then
ACTION 1
else if (CONDITION 2) then
ACTION 2
end if
In this scenario, ACTION 1 is only performed if CONDITION 1 is true.
ACTION 2 is only performed if CONDITION 1 is false and CONDITION 2 is true.
You may have as many else-if statements as necessary.
if (CONDITION 1):
ACTION 1
elif (CONDITION 2):
ACTION 2
The final form is a combination of previous if statement types. The important bit is where the "else" statement goes: At the end.
if (CONDITION 1) then
ACTION 1
else if (CONDITION 2) then
ACTION 2
else
ACTION 3
end if
You can have as many else-if statements as are necessary, as long as the else statement comes last.
if (CONDITION 1):
ACTION 1
elif (CONDITION 2):
ACTION 2
else:
ACTION 3
Below is some code used to create a simple guessing game. The game generates a random number between 1 and 10 and asks the user to guess what that number is.
import random
value = random.randint(1,10)
guess = int(input("Guess a number: "))
if (value == guess):
print("Good job! You got it!")
else:
print("Nope! Sorry, you lost!")
Be aware: A person who is under 16 will display three messages, one for being under 16, one for also being under 18, and one for also being under 25.
Enter your name? Mr. Rivard
Hello, Mr. Rivard, how old are you? 17
You can't vote, Mr. Rivard.
You can't rent a car, Mr. Rivard.
Use the information gathered to determine how much someone will weigh on each of the given planets. The relative weights for the planets are as follows:
Please enter your current earth weight: 128
I have information for the following planets:
1. Venus 2. Mars 3. Jupiter
4. Saturn 5. Uranus 6. Neptune
Which planet are you visiting? 2
Your weight would be 49.92 pounds on that planet.