Beginner's Guide

Python Control Statements

Learn Python control statements easily with examples: if, else, loops, nested conditions, break and continue in simple language.

🚦

Introduction

Imagine you're driving a car. Sometimes you go straight, sometimes you turn, and sometimes you stop. Programming works in a very similar way! A program doesn't always run in a straight line—it makes decisions and repeats actions. That's where control statements in Python come in.

Control statements help your program decide what to do next. Whether it's checking a condition, repeating a task, or stopping execution, these tools are essential for building real-world applications.

📋 Table of Contents

01What are Control Statements?
02Understanding if Statement
03Using else Statement
04Using elif Statement
05Nested Conditions
06Real-Life Example of Conditions
07Introduction to Loops
08for Loop in Python
09while Loop in Python
10Difference Between for and while
11Break Statement
12Continue Statement
13Real-Life Loop Examples
14Complete Program Example
15Common Mistakes to Avoid
1

What are Control Statements?

Control statements are used to control the flow of a program.

👉

In simple words: They help your program make decisions and repeat actions.

2

Understanding if Statement

What is if? The if statement checks a condition. If the condition is true, it runs the code.

👉 EXAMPLE
age = 18

if age >= 18:
    print("You can vote")
OUTPUT
You can vote
✔ The condition age >= 18 is true
✔ So the message is printed
3

Using else Statement

What is else? The else block runs when the condition is false.

👉 EXAMPLE
age = 15

if age >= 18:
    print("You can vote")
else:
    print("You cannot vote")
OUTPUT
You cannot vote
4

Using elif Statement

What is elif? elif means "else if". It checks multiple conditions.

👉 EXAMPLE
marks = 75

if marks >= 90:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Grade C")
OUTPUT
Grade B
5

Nested Conditions

What are Nested Conditions? When you use an if inside another if, it's called nesting.

👉 EXAMPLE
age = 20
citizen = True

if age >= 18:
    if citizen:
        print("You can vote")
OUTPUT
You can vote
6

Real-Life Example of Conditions

REAL WORLD

🏧 ATM Withdrawal System

balance = 5000
withdraw = 2000

if withdraw <= balance:
    print("Transaction Successful")
else:
    print("Insufficient Balance")
🏭 Real-Life Use: Banking systems
7

Introduction to Loops

Loops are used to repeat a block of code multiple times.

🦷

Think of it like brushing your teeth daily—you repeat the same action!

8

for Loop in Python

What is for loop? A for loop is used when you know how many times to repeat.

👉 EXAMPLE
for i in range(5):
    print("Hello")
OUTPUT
Hello
Hello
Hello
Hello
Hello
9

while Loop in Python

What is while loop? A while loop runs as long as the condition is true.

👉 EXAMPLE
i = 1

while i <= 3:
    print(i)
    i += 1
OUTPUT
1
2
3
10

Difference Between for and while Loop

FEATURE for LOOP while LOOP
Use Known repetitions Unknown repetitions
Condition Automatic Manual
11

Break Statement

What is break? break stops the loop immediately.

👉 EXAMPLE
for i in range(5):
    if i == 3:
        break
    print(i)
OUTPUT
0
1
2
12

Continue Statement

What is continue? continue skips the current iteration.

👉 EXAMPLE
for i in range(5):
    if i == 2:
        continue
    print(i)
OUTPUT
0
1
3
4
13

Real-Life Loop Examples

EXAMPLE 1

🔢 Print Even Numbers

for i in range(1, 11):
    if i % 2 == 0:
        print(i)
EXAMPLE 2

🔐 Password Retry System

attempts = 3

while attempts > 0:
    password = input("Enter password: ")
    
    if password == "1234":
        print("Access Granted")
        break
    else:
        print("Try Again")
        attempts -= 1
14

Complete Program Example

🎓 Student Result Checker
marks = int(input("Enter marks: "))

if marks >= 90:
    print("Excellent")
elif marks >= 50:
    print("Pass")
else:
    print("Fail")

print("\nCounting attempts:")

for i in range(1, 4):
    print("Attempt", i)
15

Common Mistakes to Avoid

Missing Indentation
Python uses indentation to define code blocks — always indent correctly inside if/loop bodies.
Using = instead of ==
= assigns a value, while == compares two values — use == in conditions.
Infinite Loops
A while loop without a proper exit condition will run forever and crash your program.
Forgetting Loop Conditions
Always make sure your loop has a clear condition that will eventually become false to stop execution.
🏁

Conclusion

Control statements are the backbone of programming in Python. They allow your program to think, decide, and repeat actions—just like humans do in daily life. Once you understand these concepts, you'll be able to build smarter and more interactive programs. Keep practicing and try creating your own examples—it's the best way to learn!

❓ FAQs

1. What are control statements in Python?

They control the flow of execution like decisions and loops.

2. What is the difference between if and elif?

if starts a condition, elif checks additional conditions.

3. When should I use a for loop?

Use it when you know how many times to repeat.

4. What does break do in Python?

It stops the loop immediately.

5. Can a while loop run forever?

Yes, if the condition never becomes false (infinite loop).

✦ PYTHON CONTROL STATEMENTS GUIDE ✦ BEGINNER FRIENDLY ✦
✦ THE ASCENDANT TRIAD ✦
🏆 3 game‑changing catalysts ⚡ curated for visionaries

Elevate your craft — from first spark to full-stack brilliance

🐍
Python Alchemy: Syntax to Sorcery
Unlock automation, data logic, and real‑world problem solving. Build projects that think with you — from day one.
✨ 94% mastery rate ✨
🎨
Semantic Canvas: HTML & CSS Alchemy
Architect responsive digital realms, fluid layouts, and pixel‑perfect interfaces. Become the artist of the modern web.
🌈 12+ interactive modules 🌈
JavaScript Odyssey: Frontier to Cloud
Conquer React, Node.js, databases & deployment — become a one‑person army. Build full‑stack apps that dazzle.
🔥 industry‑led bootcamp 🔥
🚀 IGNITE YOUR TRANSFORMATION →