If Statements
Contents
If Statements#
If statements give us the ability to execute different blocks of code depending on the outcome of a logical statement (or boolean value).
The syntax for an if statement is:
if condition:
block of code
where block of code
following the :
and indented is considered as the code inside the if statement. block of code
will only be executed if condition is/evaluates to true
.
Consider an if statement with code around it:
code block before
if condition:
code block inside
code block after
here code block before
will be executed first, then condition
will be evaluated. If condition
is true, code block inside
will be executed. Finally code block after
will be executed (regardless of whether or not condition
is true). Illustrated in a control flow diagram:
Worked Example
Let’s consider the problem where we want to check if one variable is greater than the other. One solution using an if statement is:
a = 3
b = 2
if a > b:
print(a, 'is greater than', b)
3 is greater than 2
If we ran the code above but with
a = 2
b = 2
then we would see nothing printed out as a > b
would evaluate to False
and the code block contained in the if statement would not be executed.
Else#
What if you wanted to execute a code block if a statement is true; and another if it’s false? The else
part of an if statement can be used for this:
if condition:
code_block_1
else:
code_block_2
If condition
evaluates to True
then code_block_1
will be executed. If, on the other hand, condition
evaluates to False
, code_block_2
will be executed.
This pseudo code can be illustrated by the control flow diagram:
Note
The else
statement cannot stand by itself. It requires a preceding if statement or loop for context.
Worked Example
Let’s take our first example and add an else
part to it:
a = 3
b = 2
if a > b:
print(a, 'is greater than', b)
else:
print(a, 'is less than or equal to', b)
3 is greater than 2
a = 1
b = 2
if a > b:
print(a, 'is greater than', b)
else:
print(a, 'is less than or equal to', b)
1 is less than or equal to 2
Elif#
Now, what if we had 2 conditions which are mutually exclusive (if one is true the other is necessarily false) and the one isn’t just the converse of the other. For this we can use the elif
part of the if statement (to be read “else if”):
if condition1:
code_block_1
elif condition2:
code_block_2
If condition 1
is false, condition 2
will be evaluated. If condition 2
is found to be true, then code block 2
will be executed, if not then control will move from the if statement. Illustrated as a control flow diagram:
Worked Example
Let’s continue with our worked example and change the else
part to be more specific:
a = 1
b = 2
if a > b:
print(a, 'is greater than', b)
elif a < b:
print(a, 'is less than', b)
1 is less than 2
a = 1
b = 1
if a > b:
print(a, 'is greater than', b)
elif a < b:
print(a, 'is less than', b)
Else After an Elif#
Now, if we want to catch the case where both the conditions in the if
and elif
parts of the if statement are false, we can use an else
part at the very end of the if statement. In pseudo code:
if condition1:
code_block_1
elif condition2:
code_block_2
else:
code_block_3
Illustrated in a control flow diagram:
Warning
The else
must always be the last part of the if statement and there can only be one.
Worked Example
Now, let’s re-introduce an else
part to our worked example:
a = 1
b = 1
if a > b:
print(a, 'is greater than', b)
elif a < b:
print(a, 'is less than', b)
else:
print(a, 'is equal to', b)
1 is equal to 1
Multiple Elif Parts#
Though you may only use one else
part in an if statement, you are not limited by how many elif
parts you wish to use. For example:
if condition1:
code_block_1
elif condition2:
code_block_2
elif condition3:
code_block_3
else:
code_block_4
This can be illustrated using a control flow diagram:
Worked Example
As an example of a script with multiple elif
parts, lets write a script that checks if a variable is a multiple of 2, 3, or 5:
var = 21
if var % 2 == 0:
print('Variable is a multiple of 2')
elif var % 3 == 0:
print('Variable is a multiple of 3')
elif var % 5 == 0:
print('Variable is a multiple of 5')
else:
print('Variable is not a multiple of 2, 3 or 5')
Variable is a multiple of 3
Note that if we put in a value that is both a multiple of 2 and 3, the script will only print out that it is a multiple of 2:
var = 6
if var % 2 == 0:
print('Variable is a multiple of 2')
elif var % 3 == 0:
print('Variable is a multiple of 3')
elif var % 5 == 0:
print('Variable is a multiple of 5')
else:
print('Variable is not a multiple of 2, 3 or 5')
Variable is a multiple of 2
This is because the check to so if it’s a multiple of 2 is placed before the check for 3 in the if
statement.