Program Flow
Learning outcomes
- What is a conditional statement?
- Learn about the
if
,elif
, andelse
statements - Learn about the
and
,or
,not
keywords - Understand the difference between
for
andwhile
loops - Learn to use
break
andcontinue
About program flow
Program flow refers to the order in which the instructions of a computer program are executed during runtime. In code, you have statements and functions, and their order (program flow) is determined by the control structres used in the program such as conditional statements (if/else), loops (for/while), and function calls.
Conditional Statements
Conditional statements are used to make decisions in the program flow. They are used to check if a certain condition is true or false. In Python, a value that holds True or False is called a boolean. If the condition is true, the code block is executed. If the condition is false, the code block is skipped. In Python, the if
statement is used to check for a condition.
if True:
print("This will be printed")
Output:
This will be printed
The if
statement can be followed by an else
statement, which executes a block of code when the condition in the if
statement is false.
if False:
print("Condition is True")
else:
print("Condition is False")
Output:
Condition is False
The if
statement can be followed by an elif
statement, which is short for "else if". It allows us to check for multiple expressions, as we can continue to stack elif
statements after each other. If the condition for the if
statement is false, it checks the condition of the next elif
statement and so on. If all the conditions are false, the code block in the else
statement is executed.
if False:
print("Condition 1 is True")
elif False:
print("Condition 2 is True")
elif True:
print("Condition 3 is True")
else:
print("All conditions are False")
Output:
Condition 3 is True
Comparisons
In Python, we can compare values (which return a boolean value) for data types such as integers, strings, and more using the following common operators:
==
(equal to)!=
(not equal to)(greater than)
<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
Examples
number = 10
if(number > 0):
print("The number is positive")
else:
print("The number is negative")
Output:
The number is positive
if(user_input == "Hello"):
print("Hello to you too!")
Boolean Operations
We can also use boolean operations to compare values.
- The
and
operator returnsTrue
if both statements are true. - The
or
operator returnsTrue
if one of the statements is true. - The
not
operator returns the opposite boolean value.
if (True and True):
print("Both statements are true")
if(True and False):
print("At least one statement is false, so this won't be printed")
if (True or False):
print("At least one statement is true")
if(False or False):
print("Both statements are false, so this won't be printed")
if (not True):
print("This won't be printed")
Output:
Both statements are true
At least one statement is true
Examples
if (number > 0 and number < 10):
print("The number is between 0 and 10")
if (user_input == "Hello" or user_input == "Hi"):
print("Hello to you too!")
Loops
Loops are used to execute a block of code multiple times. In Python, there are two types of loops: for
and while
. The loops repeat a block of code until a certain condition is met.
For Loops
The for
loop is used to iterate over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). The syntax of a for
loop is:
for item in sequence:
# do something with the item
Let's say we have a list of numbers and we want to print each number in the list. We can use a for
loop to iterate over the list and print each number.
for number in [0, 1, 2, 3, 4]:
print(number)
Output:
0
1
2
3
4
Range Function
We can also use the range()
function to create a sequence of numbers starting from 0, incrementing by 1, and stopping before a specified number.
for number in range(5):
print(number)
Output:
0
1
2
3
4
We can also specify the start, stop, and step size of the sequence using the following syntax:
range(start, stop, step_size)
If we specify only one parameter, the range()
function will start from 0 and stop before the specified number as shown in the first example. If we specify two parameters, the range()
function will start from the first parameter and stop before the second parameter. If a third parameter is specified, the range()
function will start from the first parameter, stop before the second parameter, and increment by the third parameter.
for number in range(2, 5):
print(number)
Output:
2
3
4
for number in range(0, 5, 2):
print(number)
Output:
0
2
4
Break and Continue
We can use the break
statement to stop the loop before it has looped through all the items.
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2
We can use the continue
statement to stop the current iteration of the loop and continue with the next.
for i in range(5):
if i == 3:
continue
print(i)
Output:
0
1
2
4
Nested Loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop". We can have as many nested loops as we want, but we should be careful with the values we provide as they may take a long time to execute.
for i in range(3):
for j in range(3):
print(i, j)
Output:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
While Loops
The while
loop is used to execute a set of statements as long as a condition is true. The syntax of a while
loop is:
while condition:
# do something
We have to be careful with the condition we provide as it may never be False
and the loop will never end. This is called an infinite loop.
Don't do this:
while True:
print("This will print forever")
If you ever accidentally create an infinite loop, you can press Ctrl + C
on most systems to stop the program in your terminal.
While Loop Example
We can print the numbers from 0 to 4 using a while
loop.
i = 0
while i < 5:
print(i)
i += 1
Output:
0
1
2
3
4
Break and Continue
We can similarly to the for
loop use the break
and continue
statements in a while
loop.
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
Output:
0
1
2
i = 0
while i < 5:
if i == 3:
i += 1
continue
print(i)
i += 1
Output:
0
1
2
4
Exercises
- Loop through a range of 10 numbers and print the even ones.
- Create a list of 5 values of different types (doesn't matter which types you're going to use as long as they're not repeating too much). Loop through it and print each value as an item in a numbered list. For example, the first value in your list with the index
0
should be displayed as1. {value}
; the second value with index1
as2. {value}
, and so on. - Solve the FizzBuzz problem. (Here's a LeetCode link if you need instructions.)
- Print Characters from a String at Each Even Index
- Merge Two Lists
- Reverse the Strings for Each Item in the Array
- Check if the Input is a Palindrome
Knowledge Check
- Explain what
if...else
does. - Which values are considered true in Python and which are false?
- What does the
range()
function do and what are its arguments? - What are two main ways to interrupt the current cycle of a loop and what's the difference between them?