Here is a clear, complete, and exam-oriented explanation of Python Control Statements, perfect for BCA/MCA/B.Tech students.
Python Control Statements
Control statements in Python are used to control the flow of execution in a program.
They help in decision-making, looping, and jumping from one part of the code to another.
In Python, control statements are mainly of three types:
- Decision Control Statements
- Loop Control Statements
- Jump (Transfer) Control Statements
Let’s discuss each of them in detail.
1. Decision Control Statements
These statements help the program choose a path based on conditions.
Includes:
ifif-elseif-elif-else- Nested
if - Short-hand
if
Example:
age = 18
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")
Used when you want to make decisions in the program.
2. Loop Control Statements
These statements are used to repeat a block of code multiple times.
Includes:
forloopwhileloop- Loop with
else
2.1 for Loop
Used to iterate over a sequence.
for i in range(5):
print(i)
2.2 while Loop
Runs as long as the condition remains True.
i = 1
while i <= 5:
print(i)
i += 1
2.3 else with Loops
Executed only if loop ends normally (without break).
for i in range(3):
print(i)
else:
print("Loop ended normally")
3. Jump (Transfer) Control Statements
These statements change the normal flow of the loop.
Includes:
breakcontinuepass
3.1 break Statement
Used to exit the loop immediately.
Example:
for i in range(10):
if i == 5:
break
print(i)
Output:
0 1 2 3 4
3.2 continue Statement
Skips the current iteration and continues the loop.
Example:
for i in range(6):
if i == 3:
continue
print(i)
Output:
0 1 2 4 5
3.3 pass Statement
Does nothing.
Used as a placeholder in loops, conditions, classes, or functions.
Example:
for i in range(5):
if i == 3:
pass # No action
print(i)
Summary Table of Control Statements
| Category | Statements | Purpose |
|---|---|---|
| Decision Control | if, if-else, if-elif-else | Make decisions |
| Looping Control | for, while, else | Repeat code |
| Jump Control | break, continue, pass | Modify flow inside loops |
Real-Life Example: Student Marks Evaluation
marks = 85
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
elif marks >= 50:
print("Average")
else:
print("Fail")
Real-Life Example: Search in a List
names = ["Ram", "Sham", "Mohan"]
for name in names:
if name == "Sham":
print("Found!")
break
else:
print("Not found")
Conclusion
Python control statements allow you to:
✔ Make decisions
✔ Repeat tasks
✔ Jump or skip parts of code
Understanding these concepts is essential for writing logical and efficient Python programs.
