20. Introduction to control flows#

Author: Tue Nguyen

20.1. Outline#

  • What is a control flow?

  • Types of control flows in Python

  • Sequential flows

  • Branching flows

  • Looping flows

20.2. What is a control flow#

  • In programming, a control flow is the order in which the program’s code executes

  • In Python, there are 3 types of control flows

    • Sequential: execute linearly (top-down)

    • Branching: check a condition and decide which action (branch) to execute

    • Looping: repeat a block of code until some condition is met

  • We will learn each type of control flows in detail in the next lectures

20.3. Sequential flows#

  • This is the default mode when you write Python code

  • The statements that are written first will be executed first

# Statements will be run in the order they are written
print("This will run first")
print("This will run second")
This will run first
This will run second

20.4. Branching flows#

  • A branching statement involves checking some condition and based on the checking result, Python will choose appropriate action (branch) to proceed

  • Branching in Python is implemented using the if statement which you are already familiar with

  • Python 3.10+ introduces another way to implement branching through the match statement

  • Let’s consider some simple examples

a) Ex 1: print "positive" if a number is positive

# Case 1: condition is True
num = 3
if num > 0:
    print("positive")
positive
# Case 2: condition is False
num = -10
if num > 0:
    print("positive")

b) Ex 2: print "odd" if an integer is odd; otherwise, print "even"

# Case 1: condition is True
num = 3
if num % 2 != 0:
    print("odd")
else:
    print("even")
odd
# Case 2: condition is False
num = -10
if num % 2 != 0:
    print("odd")
else:
    print("even")
even

20.5. Looping flows#

  • A loop statement repeats a block of code until some condition is met

  • In Python, we can write loops using for or while

  • We will learn them in detail in the next lectures

  • For now, just consider some simple examples with for

# Init a list of numbers
numbers = [1, 5, -3, -7, 0, 2]
numbers
[1, 5, -3, -7, 0, 2]
# Ex 1: print all element
# In this example, the print statement is repeated 6 times
# The loop only ends when it reach the end of the list (the condition)
for e in numbers:
    print(e)
1
5
-3
-7
0
2
# Ex 2: print out only positive numbers
# In this example, we only see three elements being printed out
# But the code block under for is still exectued 6 times
# Python compare each element of numbers with 0 in each iteration
# But only 3 times the condition for if is True
# Thus, we only see three positive elements being printed out
for e in numbers:
    if e > 0:
        print(e)
1
5
2

20.6. Summary#

  • A control flow is the order in which the program’s code executes

  • Python has 3 types of control flows

    • Sequential

      • Default mode

      • Code executes linearly (top-down)

    • Branching

      • Check a condition and decide which action (branch) to execute

      • Implement branching using if or match

    • Loop

      • Repeat a block of code until some condition is met

      • Implementing loops using for or while