Control Flow

Comprehensive study notes, diagrams, and exam preparation for Control Flow.

Control Flow

Definition

Control flow is the mechanism that determines the sequence in which instructions are executed in a program, using structures such as sequence, selection, and iteration, along with statements like if, else, switch, for, while, break, and continue.

It is the backbone of program logic because it directs how the program reacts to conditions, repeats tasks, and manages different execution paths.


Main Content

1. Sequential Control Flow

Sequential control flow

  • means instructions execute one after another in the exact order they are written, unless a control statement changes the path.
  • This is the default flow in most programs and forms the base of all other control structures.

In sequential execution, each statement completes before the next one begins. For example:

Step 1: Read input
Step 2: Process input
Step 3: Display output

If a program contains:

a = 5
b = 10
c = a + b
print(c)

the program first assigns values to a and b, then calculates c, and finally prints the result.

Why it matters:

  • It provides a predictable and clear execution path.
  • It is easy to understand and debug.
  • Most algorithms begin with sequential steps before adding decisions or repetition.

Example in real life: Making tea usually follows a sequence:

  1. Boil water
  2. Add tea leaves
  3. Add milk or sugar
  4. Pour into cup

This same idea applies to program instructions.


2. Decision-Making Control Flow

Decision-making control flow

  • lets a program choose between different paths based on whether a condition is true or false.
  • Common decision structures include if, if-else, else-if ladder, and switch/case.

This type of control flow is used whenever a program must respond differently to different situations.

If statement

Used when a block should run only if a condition is true.

age = 18
if age >= 18:
    print("You are eligible to vote")

If-else statement

Used when there are two possible outcomes.

number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Else-if ladder

Used when multiple conditions must be checked in order.

marks = 82
if marks >= 90:
    print("A")
elif marks >= 75:
    print("B")
elif marks >= 60:
    print("C")
else:
    print("D")

Switch-case concept

In many languages, switch is used to compare one value against many possible cases.

switch(day)
  case 1 -> Monday
  case 2 -> Tuesday
  case 3 -> Wednesday

Why it matters:

  • It enables programs to make logical choices.
  • It helps handle user input, validation, and branching behavior.
  • It reduces unnecessary execution of irrelevant code.

Example in practice: An ATM system may:

  • allow withdrawal if balance is sufficient,
  • show an error if balance is too low,
  • request another action if the user presses a different menu option.

3. Repetition Control Flow

Repetition control flow

  • allows a block of code to run multiple times.
  • It is also called iteration or looping.
  • Common loops include for loop, while loop, and do-while loop.

Loops are useful when repeating the same task many times would otherwise require writing the same code repeatedly.

For loop

Used when the number of repetitions is known or when iterating through a collection.

for i in range(1, 6):
    print(i)

This prints numbers from 1 to 5.

While loop

Used when repetition depends on a condition and the number of iterations is not known in advance.

count = 1
while count <= 5:
    print(count)
    count += 1

Do-while loop concept

A do-while loop runs the block at least once before checking the condition. Some languages support it directly; others simulate it.

Why it matters:

  • Saves time and avoids repetitive coding.
  • Makes it easy to process lists, tables, menus, and repeated tasks.
  • Supports dynamic behavior such as waiting for valid input.

Nested loops A loop inside another loop is called a nested loop. It is often used in patterns, tables, and matrix operations.

for row in range(3):
    for col in range(4):
        print("*", end=" ")
    print()

This can create a rectangle of stars.

Loop control statements

break

  • : exits the loop immediately

continue

  • : skips the current iteration and moves to the next one

pass

  • : does nothing, often used as a placeholder

Example:

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

Working / Process

1. Program starts with sequential execution

  • The computer begins running statements one by one from top to bottom.
  • Initial values are assigned, input may be read, and basic processing begins.

2. Conditions are evaluated

  • The program checks whether a condition is true or false.
  • Based on the result, it selects one path from several possible paths.
  • Example: if a student’s marks are above 50, print “Pass”; otherwise print “Fail”.

3. Repeated actions are performed until the stopping condition is met

  • Loops continue running while a condition remains true or for a fixed number of times.
  • The loop body updates variables each time so that the program can eventually stop.
  • Example flow:
Start
  |
  v
Check condition?
  |yes
  v
Execute block
  |
  v
Update value
  |
  v
Check again
  |no
  v
End

This process ensures that a program can make decisions, repeat tasks, and finish correctly without getting stuck.


Advantages / Applications

Makes programs flexible and intelligent

  • by allowing them to react to different inputs and situations.

Reduces code duplication

  • because loops can repeat tasks instead of rewriting the same instructions many times.

Supports real-world problem solving

  • such as login systems, calculators, menu-driven programs, games, sorting, searching, and validation.

Control flow is used almost everywhere in software:

  • in banking systems for approval checks,
  • in websites for form validation and page navigation,
  • in games for scoring, movement, and level progression,
  • in scientific programs for repeated calculations,
  • in operating systems for task scheduling and decision handling.

Summary

  • Control flow decides the order in which a program runs.
  • It mainly works through sequence, decision, and repetition.
  • It helps programs make choices, repeat work, and behave logically.
  • Important terms to remember: sequence, if, else, switch, loop, iteration, break, continue