Skip to content
Home » Notes » 🧭Control Structures — If-Else, Loops & Logical Flow

🧭Control Structures — If-Else, Loops & Logical Flow

  • by

💡 Introduction

Every program must make decisions and repeat tasks — just like humans.
That’s what control structures do in coding.

They help your program decide what to do next depending on certain conditions or rules.

There are two main types of control structures:

  1. Decision Making (If-Else Statements)
  2. Repetition (Loops)

🧠 1. Decision Making — If, Else If, Else

Decision-making allows your code to choose between multiple actions based on conditions.

🧩 Example (Python)

age = 18

if age >= 18:
    print("You are an adult.")
else:
    print("You are not an adult.")

✅ If the condition (age >= 18) is True, the program runs the first block.
❌ If it’s False, it runs the second block.


🧩 Multiple Conditions (Else If / Elif)

Sometimes, you need to check more than one condition.

Example (Python):

score = 75

if score >= 80:
    print("Grade: A")
elif score >= 60:
    print("Grade: B")
else:
    print("Grade: C")

💬 The program checks each condition from top to bottom until one is True.


💻 Example (C++)

int score = 45;

if (score >= 80)
    cout << "Grade: A";
else if (score >= 60)
    cout << "Grade: B";
else
    cout << "Grade: C";

🔁 2. Repetition — Loops

Loops are used to repeat a block of code multiple times without writing it again.
This saves time and makes your program efficient.

There are two main loop types:

  • For Loop
  • While Loop

🔹 For Loop

Used when you know how many times you want to repeat something.

Example (Python):

for i in range(5):
    print("Hello EduTech!")

✅ Output:

Hello EduTech!
Hello EduTech!
Hello EduTech!
Hello EduTech!
Hello EduTech!

💬 The loop runs 5 times — from 0 to 4.


🔹 While Loop

Used when you don’t know exactly how many times to repeat — it continues as long as a condition is true.

Example (Python):

count = 1

while count <= 5:
    print("Count:", count)
    count += 1

✅ The loop stops automatically when count becomes greater than 5.


🔹 Break and Continue

KeywordFunction
breakStops the loop immediately
continueSkips the rest of the loop and goes to the next iteration

Example (Python):

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

✅ Output: 1 2 4 5 (because 3 was skipped)


🧩 3. Logical Flow

Logical flow is the path your program follows when running.
It depends on the sequence of statements, conditions, and loops.

You can control the flow using:

  • Sequential flow → Code runs line by line.
  • Decision flow → Uses if, elif, else.
  • Repetition flow → Uses for or while loops.

💡 Good programmers design clear logical flows so that code is easy to understand and debug.


🧠 Summary

ConceptDescription
If-ElseMakes decisions based on conditions
For LoopRepeats code a fixed number of times
While LoopRepeats code while a condition is true
Break / ContinueControl the flow inside loops