DEV Community

Cover image for Control Statements in Python (if, else, loops)
shalini
shalini

Posted on

Control Statements in Python (if, else, loops)

When you start learning Python, one of the most important concepts you must understand is control statements.

Think about real life πŸ‘‡

βœ“ If it rains, you take an umbrella
βœ“ If you are hungry, you eat
βœ“ You repeat actions like walking or studying

Programming works in the same way.

Your program needs to:

βœ“ Make decisions
βœ“ Execute specific actions
βœ“ Repeat tasks

πŸ‘‰ That’s where control statements in Python come into play.

What are Control Statements in Python?

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

They help your program:

βœ“ Make decisions
βœ“ Execute specific blocks of code
βœ“ Repeat tasks multiple times

πŸ‘‰ In simple terms:
Control statements decide what to execute and when to execute

Why Control Statements are Important

Understanding Python control statements is essential for every developer.

They help you:

βœ“ Build strong programming logic
βœ“ Develop real-world applications
βœ“ Crack coding interviews
βœ“ Improve problem-solving skills

Without control statements, programs run line by line without logic.

Types of Control Statements

Control statements are mainly divided into:

βœ“ Decision-making (if, else)
βœ“ Looping (for, while)

1. Decision Making using if, else

** if Statement**

Executes code only when a condition is true.

age = 18

if age >= 18:
    print("You are eligible to vote")
Enter fullscreen mode Exit fullscreen mode

** if-else Statement**

Handles both true and false conditions.


age = 16

if age >= 18:
    print("Eligible")
else:
    print("Not Eligible")
Enter fullscreen mode Exit fullscreen mode

** if-elif-else Statement**

Used when multiple conditions exist.

m

arks = 75

if marks >= 90:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Grade C")
Enter fullscreen mode Exit fullscreen mode

** 2. Loops in Python**

Loops repeat a block of code multiple times.

for Loop

Used when iterations are known.


for i in range(1, 6):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

βœ“ 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

while Loop

Used when iterations are not known.

i = 1

while i <= 5:
    print(i)
    i += 1
Enter fullscreen mode Exit fullscreen mode

break Statement

Stops the loop immediately.

for i in range(1, 6):
    if i == 3:
        break
    print(i)
Enter fullscreen mode Exit fullscreen mode

continue Statement

Skips the current iteration.

for i in range(1, 6):
    if i == 3:
        continue
    print(i)
Enter fullscreen mode Exit fullscreen mode

Key Concepts to Remember

βœ“ if statement
βœ“ if-else and if-elif-else
βœ“ for loop
βœ“ while loop
βœ“ break and continue
βœ“ indentation (very important in Python)

Real-World Use Cases

Control statements are used everywhere.

Login Systems

βœ“ Validate credentials using if-else

E-Commerce

βœ“ Loop through products using for loops

Banking Systems

βœ“ Check balance before transactions

Games

βœ“ Repeat actions and apply conditions

Advantages

βœ“ Improves logic building
βœ“ Enables decision-making
βœ“ Reduces code repetition
βœ“ Makes programs dynamic

Disadvantages

βœ“ Can become complex
βœ“ Risk of infinite loops
βœ“ Hard to debug for beginners

Combined Example

number = 5

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

for i in range(1, 4):
    print("Loop:", i)
Enter fullscreen mode Exit fullscreen mode

** Tools to Practice**

βœ“ Python IDLE
βœ“ VS Code
βœ“ PyCharm
βœ“ Jupyter Notebook
βœ“ Google Colab

Common Mistakes

βœ“ Missing indentation
βœ“ Wrong conditions
βœ“ Infinite loops
βœ“ Confusing = and ==
βœ“ Not understanding flow

Interview Questions

What are control statements?

βœ“ Control program flow

if vs elif?

βœ“ elif handles multiple conditions

What is a loop?

βœ“ Repeats code

for vs while?

βœ“ for β†’ known iterations
βœ“ while β†’ unknown

What is break?

βœ“ Stops loop

FAQs

Is indentation important?

βœ“ Yes

Multiple elif allowed?

βœ“ Yes

What if condition is false?

βœ“ else runs

Infinite loop?

βœ“ Loop that never ends

Which loop is better?

βœ“ Depends on use case

Final Thoughts

Understanding control statements in Python (if, else, loops) is essential for programming.

βœ“ Builds strong logic
βœ“ Helps solve real problems
βœ“ Used in every application

Practice daily, write small programs, and improve step by step.

That’s how you master Python.

Top comments (0)