Flow Control Statements
Elements of Flow Control
There are two important elements of Flow Control that we will cover, before diving into Flow Control Statements. Flow Control statements have a condition, and are followed by a block of code, referred to as clause.
Conditions
The Boolean expressions we have seen so far can be used as conditions. In general, they are expressions, conditions is just a more specific name we give expressions used in the context of flow control statements. Conditions always evaluate down to a Boolean value, True
or False
. A flow control statement decides what to do based on these evaluations.
Note: Almost every flow control statement uses a condition.
Task: Find a flow control statement that doesn't use a condition. Spend some time to think about it at the end of this unit.
Blocks of Code (Clause)
In Python, unlike other languages, indentation is used instead of various other special characters like ()
or {}
for the purpose of making a code block. Or in other words, you can tell when a block begins and ends from the indentation of the lines of code. There are three rules for these blocks:
- Blocks begin when the indentation increases.
- Blocks can contain other blocks.
- Blocks end when the indentation decreases to zero or to a containing block’s indentation.
Example:
name = 'Zoe'
password = 'student'
if name == 'Zoe':
print('Hello Zoe')
if password == 'student':
print('Access granted.')
else:
print('Wrong password.')
You don't need to understand the code above, but notice how the code is written. All the code is inside the first if name == 'Zoe':
statement. There is another block that starts from if password == 'student':
.
Note: In this case, the code doesn't execute every line. In case one of the expressions/conditions evaluate to
False
, the underlying code is not executed during the run.
IF Statements
One of the most common flow control statement is the if
statement. The condition executes if the evaluation is True
, otherwise not. To put this in more simple terms, we could read this as "If this condition is true, execute the code in the clause" in English. In Python, an if
statement consists of the following:
- The
if
keyword - A condition (an expression that evaluates to
True
orFalse
) - A colon (:)
- An indented block, starting from the next line (
if
clause)
Example:
if name == 'Alex':
print('Successful Pass')
Here, Successful Pass
will be printed if
name is Alex
.
ELSE Statements
The if
clause can be followed by an else
clause optionally. This clause will only be executed if the condition for the if
block evaluates to False
. In simple words, we could write this as, "If this condition is true, execute this code. Or else, execute that code". An else
statement doesn’t have a condition, and in Python, an else statement always consists of the following:
- The
else
keyword - A colon (:)
- An indented block, starting from the next line (
else
clause)
Example:
if name == 'Alex':
print('Successful Pass')
else:
print('Fail')
Here, Fail
will be printed if
name is not Alex
.
ELIF Statements
One thing you might think about is, what if I have more than 1 condition that can happen and I can't use them in the same if
clause? For this purpose, there is the elif
clause. It can also be called as the Else if clause. It provides another condition that is checked only if all of the previous conditions were False. In Python, an elif
statement always consists of the following:
- The
elif
keyword - A condition (an expression that evaluates to
True
orFalse
) - A colon (:)
- An indented block, starting from the next line (
else
clause)
Example:
if name == 'Alex':
print('Successful Pass')
elif name == 'Becky':
print('Good job!')
else:
print('Fail')
Here, Good job
will be printed if
name is 'Becky'.
REMEMBER: In case you have two
elif
statements that can be true, the first one in the order will be executed and the rest of them will be skipped.