If statements¶
Definition¶
An if statement allows you to run different blocks of code depending on a condition.
Note
if statements can be put inside if, elif, or else statements. When this happens they are called nested if statements.
Easy example¶
playerOne = input('Type in your name:')
playerTwo = input('Type in the name of your opponent:')
if playerOne == playerTwo:
print('Wow! You both have the same name')
Type in your name: Harry
Type in the name of your opponent: Harry
Wow! You both have the same name
Syntax¶
if condition:
print('this block of code runs if condition is True')
elif condition2:
print('this block of code runs if condition2 is True')
elif condition3:
print('this block of code runs if condition3 is True')
else:
print('this block of code runs if none of the other conditions were True')
Note
elif stands for ELSE IF
Note
You can have as many elif statements as you want.
Note
elif statements and the else statement are optional.
Examples¶
Example 1 - IF with Boolean only¶
gameOver = True #The gameOver variable is a flag which stores True if the game has finished.
if gameOver:
print('Sorry - you lost')
Sorry - you lost
Warning
Many beginners will program this as if gameOver == True:. Although this works, it is unnecessary. You only need the condition to end up as True or False. In this case, gameOver stores either True or False, so there is no need to do a comparison.
Example 2 - IF with comparison operator¶
This program would be used to print a message for someone under the age of 18.
age = 15
if age < 18:
print('You are not an adult yet')
You are not an adult yet
Note
If age had the value of 18 or higher, then nothing would be output from this program.
Example 3 - IF with logical operator¶
This program would be used to print a message for someone between the ages of 13 and 19.
age = 15
if age >= 13 and age <= 19:
print('You are a teenager')
You're a teenager
Note
Note how you need to write out the full condition out each time. age >= 13 and <= 19 will not work.
Example 4 - IF with ELSE¶
This program would be used to print a different message depending on the time of day which would be calulated somewhere else.
hourOfDay = 13
if hourOfDay < 12:
print('Good morning')
else:
print('Good afternoon')
Good afternoon
Example 5 - IF with ELSE IF and ELSE¶
This program would be used to print a different message depending on whether it was morning, afternoon or evening. The hourOfDay would be calculated elsewhere in the program.
hourOfDay = 19
if hourOfDay < 12:
print('Good morning')
elif hourOfDay < 18:
print('Good afternoon')
else:
print('Good evening')
Good evening
Example 6 - IF with ELSE IF only¶
This program would print different messages for different values stored in gameRating. It could easily be adapted using more elif statements.
gameRating = 9
if gameRating == 10:
print('Perfect game')
elif filmRating == 9:
print('Good game')
Good game
Note
If gameRating is not 9 or 10 then nothing will be displayed.
Example 7 - Nested IF statements¶
This program first checks if gameRunning is True. It then checks the value stored in difficulty. Based on each of these it will give different messages.
gameRunning = True
difficulty = 9
if gameRunning:
if difficulty == 10:
print('This game is going to be really hard')
else:
print('You can still progress')
else:
if difficulty == 10:
print('That was really hard')
else:
print('Keep trying')
You can still progress
Key points¶
Note
To check two conditions you can use logical operators with one if statement, or a nested if statement. In general, it is better where possible to check with logical operators. For example: if age <= 19 and age >= 13.
Note
IF statments are also known as selection statements. In other languages there are also selection statements called “CASE-SELECT”. These are not available in Python.