Unit 2 • Lesson 1

Understanding Control Flow

Overview

Learn what control flow means and how Python decides which parts of your program to run. This section introduces the concept of conditional execution and repetition, showing how logic determines the path your program follows.

Beginner 10–15 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What control flow means: Understand how programs make decisions and repeat actions.
  • Sequential execution: Learn how Python normally runs code line by line.
  • Conditional execution: Discover how programs can choose different paths based on conditions.
  • Repetition: See how loops let programs repeat actions efficiently.

Why Control Flow Matters

So far, your programs have run from top to bottom, doing the same thing every time. Control flow lets your programs make decisions and adapt to different situations. This is what makes programs interactive and useful.

Step 1: What Is Control Flow?

Control flow is how your program decides which code to run and when. Think of it like a flowchart: your program can take different paths depending on conditions, and it can repeat actions multiple times.

Sequential Execution

By default, Python runs code from top to bottom, one line at a time. This is called sequential execution.

print("First")
print("Second")
print("Third")

Always runs in order: First, Second, Third

Control Flow Changes This

With control flow, your program can skip lines, repeat lines, or choose different paths based on conditions.

if age >= 18:
    print("Adult")
else:
    print("Minor")

Runs different code based on age

Key Concept: Control flow gives your programs the ability to make decisions and repeat actions. Without it, every program would do exactly the same thing every time it runs. With control flow, programs become dynamic and responsive.

Step 2: Types of Control Flow

Python provides three main types of control flow structures:

1

Conditional Statements (if/else)

These let your program make decisions. If a condition is true, run one block of code. If false, run a different block (or skip it).

if temperature > 80:
    print("It's hot!")
else:
    print("It's cool.")
2

Loops (for/while)

These let your program repeat actions. Instead of writing the same code multiple times, you can use a loop to repeat it.

for i in range(5):
    print("Hello")
3

Control Statements (break/continue)

These let you modify how loops work. You can exit early or skip iterations when needed.

for i in range(10):
    if i == 5:
        break  # Exit loop early

Step 3: Real-World Analogy

Think of control flow like making decisions in daily life:

Daily Decision Making
# Morning routine
if it_is_raining:
    take_umbrella()
else:
    leave_umbrella_at_home()

# Repeat an action
for each_day in week:
    go_to_work()
    come_home()
    sleep()

Programs Make Decisions Too

Just like you decide what to do based on conditions (is it raining?), programs can check conditions (is the user logged in?) and respond accordingly. This makes programs useful and interactive.

Mini Practice #1: Sequential Execution

Try It Yourself

Try running this code. Notice how Python executes it line by line, in order:

Press Run to see output

What happened? Python ran each line in order, from top to bottom. This is sequential execution - the default way Python runs code. When you run a program, Python starts at the first line, executes it, then moves to the second line, executes it, and so on until it reaches the end. This predictable order is important because it means your code will always run the same way unless you use control flow to change it.

Step 4: Why We Need Control Flow

Without control flow, programs are very limited. Here's why control flow is essential:

❌ Without Control Flow

Programs always do the same thing. They can't adapt to different situations or user input.

print("Welcome!")
print("You are logged in.")
print("Here are your files.")

Always prints the same thing, even if user isn't logged in!

✅ With Control Flow

Programs can check conditions and respond appropriately. They adapt to different situations.

if user_logged_in:
    print("Welcome!")
    print("Here are your files.")
else:
    print("Please log in.")

Checks if user is logged in first!

Step 5: What's Coming Next

In this unit, you'll learn:

  • Comparison and logical operators: How to compare values and combine conditions
  • if statements: How to make decisions in your code
  • Loops: How to repeat actions efficiently
  • Practical examples: How to combine everything to build useful programs

Ready to Start: Control flow is one of the most important concepts in programming. Once you master it, you'll be able to write programs that make decisions, handle different situations, and repeat actions automatically. Let's begin!

End-of-Lesson Exercises

Exercise 1: Sequential Code

Write code that prints three messages in order: "First message", "Second message", "Third message". Run it and observe the order.

Use three print statements, one after another.

Write your code above and click "Check Answer" to verify it's correct.