Control Flow

The mechanisms that determine which instructions execute and in what order — how a program makes decisions (conditionals) and repeats actions (loops) instead of running every line exactly once from top to bottom.


What is it?

Without control flow, a program would execute every line once, in order, and stop. That is useful for about three seconds. Real programs need to make decisions (“if the user is logged in, show the dashboard”) and repeat actions (“process every item in this list”).1

Control flow is how you give a program these abilities. It is present in every programming language, and the logic is identical everywhere — only the syntax changes.

Two structures cover almost all control flow:

  • Conditionals (if/else) — execute different code depending on whether a condition is true or false
  • Loops (for, while) — repeat a block of code until a condition is met

In plain terms

Control flow is the traffic system of a program. Traffic lights (conditionals) decide whether to go or stop. Roundabouts (loops) keep traffic circling until an exit is reached. Without traffic management, everything moves in a single straight line — or collides.


At a glance


How does it work?

Conditionals: the fork in the road

An if statement evaluates a condition — an expression that is either true or false. If true, one block of code runs. If false, an alternative block runs (or nothing happens).2

if temperature > 30:
    turn_on_fan()
else:
    turn_off_fan()
graph TD
    C{temperature > 30?} -->|true| A[Turn on fan]
    C -->|false| B[Turn off fan]

    style C fill:#e8b84b,color:#fff
    style A fill:#5cb85c,color:#fff
    style B fill:#e74c3c,color:#fff

Most languages also offer else if (or elif) for multiple conditions:

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

Think of it like...

A road sign with directions. “If going to Lausanne, take the left fork. If going to Geneva, take the right fork.” The program reads the sign (evaluates the condition) and follows the appropriate path.

Loops: the washing machine

A loop repeats a block of code either a fixed number of times or until a condition changes.3

The for loop — repeat for each item in a collection or a fixed number of times:

for name in ["Marie", "Luc", "Sophie"]:
    print("Hello, " + name)

This prints three greetings — one per name. The loop knows how many times to repeat because the list has three items.

The while loop — repeat as long as a condition is true:

attempts = 0
while attempts < 3:
    password = ask_user()
    if password == correct:
        grant_access()
        break
    attempts = attempts + 1

The loop continues until either the password is correct or three attempts are used. The break statement exits the loop early.

graph LR
    S[Start] --> CH{Condition?}
    CH -->|true| A[Execute body]
    A --> CH
    CH -->|false| E[Continue]

    style CH fill:#e8b84b,color:#fff
    style A fill:#4a9ede,color:#fff
    style E fill:#5cb85c,color:#fff

The infinite loop

A loop whose condition never becomes false runs forever. This is an infinite loop — one of the most common bugs in programming:4

while true:
    print("This never stops")

When your computer “freezes” or a program becomes unresponsive, an infinite loop is often the cause. Every programmer writes one eventually. The skill is recognising the pattern and ensuring your loop has a reachable exit condition.


Why do we use it?

Key reasons

1. Decisions. Programs that cannot branch based on conditions can only do one thing. Conditionals let a program respond differently to different inputs.

2. Repetition. Processing a list of 10,000 items without loops would require 10,000 copies of the same code.

3. Interaction. User interfaces, games, and servers run in loops — continuously checking for input, processing it, and responding. Without loops, a program runs once and exits.

4. Search and filtering. Finding an item in a list, filtering records by criteria, validating input — all require iterating through data with conditions.


When do we use it?

  • Every non-trivial program uses both conditionals and loops
  • Conditionals whenever the program must behave differently based on data — user permissions, input validation, error handling
  • For loops when processing collections — lists, files, database results
  • While loops when the number of repetitions is not known in advance — waiting for input, retrying a failed operation

Rule of thumb

If you know how many times to repeat, use for. If you are waiting for a condition to change, use while. If neither applies, you may not need a loop.


How can I think about it?

The traffic system

Control flow is a traffic system. Without it, every car goes straight at the same speed — no turns, no stops, no choices.

Conditionals are traffic lights: red means stop (skip this block), green means go (execute this block). The light checks a condition (is there cross traffic?) and decides.

Loops are roundabouts: traffic circles until the right exit appears. A for loop is a roundabout with a fixed number of exits (one per item). A while loop circles until a specific exit condition is met.

The morning routine

Your morning is full of control flow. You check conditions without thinking: “If it is raining, take an umbrella. Else, wear sunglasses.” You loop through repetitive actions: “For each item on the breakfast table, put it in the dishwasher.”

You even have nested logic: “While there is coffee in the pot, pour a cup. If the cup is full, stop pouring.” Programming control flow is just making these everyday decisions explicit enough for a machine that cannot infer anything.


Concepts to explore next

ConceptWhat it coversStatus
variablesThe data that conditions evaluate and loops modifycomplete
functionsReusable blocks that often contain control flowcomplete
data-typesBooleans — the type that drives conditionalscomplete

Check your understanding


Where this concept fits

Position in the knowledge graph

graph TD
    SD[Software Development] --> CF[Control Flow]
    SD --> VAR[Variables]
    SD --> FN[Functions]
    CF -.->|uses| VAR
    FN -.->|contains| CF

    style CF fill:#4a9ede,color:#fff

Related concepts:

  • variables — control flow evaluates variables (conditions) and modifies them (loop counters)
  • functions — most functions contain control flow logic
  • data-types — booleans are the type that conditionals evaluate

Sources


Further reading

Resources

Footnotes

  1. Sebesta, R. (2019). Concepts of Programming Languages. 12th ed. Pearson.

  2. Educative. (2024). What Are the Basic Fundamental Concepts of Programming?. Educative.

  3. TripleTen. (2024). Basic Coding Concepts. TripleTen.

  4. Wikipedia. (2026). Infinite loop. Wikipedia.