Control Flow in Python
Control flow determines the order in which Python executes statements in a program.
Normally, Python executes code from top to bottom. Control flow statements allow us to make decisions, repeat code, or change the normal execution.
Main Types of Control Flow
-
Conditional Statements
ifif-elseif-elif-else- Nested
if
-
Looping Statements
forwhile
-
Loop Control Statements
breakcontinuepass
1. if Statement
The if statement executes code when a condition is True.
Syntax
if condition: # code
Example
age = 20 if age >= 18: print("You are an adult")
Output:
You are an adult
Python uses indentation to define the code inside the if statement.
2. if-else Statement
Use else when you want to execute one block if the condition is True and another block if it is False.
age = 16 if age >= 18: print("Adult") else: print("Minor")
Output:
Minor
Flow
Condition | +----+----+ True False | | if block else block
3. if-elif-else
Use elif when there are multiple conditions.
marks = 75 if marks >= 80: print("A") elif marks >= 60: print("B") elif marks >= 40: print("C") else: print("Fail")
Output:
B
Python checks the conditions from top to bottom and executes the first matching condition.
4. Multiple elif
You can use multiple elif statements.
marks = 92 if marks >= 90: print("Excellent") elif marks >= 80: print("Very Good") elif marks >= 60: print("Good") elif marks >= 40: print("Pass") else: print("Fail")
Output:
Excellent
5. Nested if
An if statement inside another if statement is called a nested if.
age = 20 citizen = True if age >= 18: if citizen: print("Eligible")
Output:
Eligible
6. Using Logical Operators
Conditions can be combined using and, or, and not.
and
Both conditions must be true.
age = 20 has_id = True if age >= 18 and has_id: print("Allowed")
or
At least one condition must be true.
day = "Sunday" if day == "Saturday" or day == "Sunday": print("Weekend")
not
Reverses the condition.
logged_in = False if not logged_in: print("Please login")
7. for Loop
A for loop is used to repeat code for each item in a sequence or iterable.
for i in range(5): print(i)
Output:
0 1 2 3 4
8. for Loop with a List
students = ["Ram", "Shyam", "Hari"] for student in students: print(student)
Output:
Ram Shyam Hari
9. for Loop with a String
A string can also be iterated character by character.
name = "Python" for letter in name: print(letter)
Output:
P y t h o n
10. range()
range() is commonly used with for loops.
range(stop)
for i in range(5): print(i)
Output:
0 1 2 3 4
range(start, stop)
for i in range(2, 6): print(i)
Output:
2 3 4 5
range(start, stop, step)
for i in range(2, 10, 2): print(i)
Output:
2 4 6 8
11. while Loop
A while loop repeatedly executes code while a condition is true.
count = 1 while count <= 5: print(count) count += 1
Output:
1 2 3 4 5
Important
Make sure the condition eventually becomes False.
Otherwise, you can create an infinite loop.
while True: print("Hello")
This continues forever until the program is stopped.
12. break
break immediately stops a loop.
for i in range(10): if i == 5: break print(i)
Output:
0 1 2 3 4
When i becomes 5, the loop stops.
13. continue
continue skips the current iteration and moves to the next iteration.
for i in range(5): if i == 2: continue print(i)
Output:
0 1 3 4
The number 2 is skipped.
14. pass
pass does nothing.
It is used when you need a statement syntactically but don't want to write the code yet.
for i in range(5): if i == 2: pass print(i)
Output:
0 1 2 3 4
Unlike continue, pass does not skip the iteration.
15. Nested Loops
A loop inside another loop is called a nested loop.
for i in range(3): for j in range(2): print(i, j)
Output:
0 0 0 1 1 0 1 1 2 0 2 1
Nested loops are commonly used for tables, patterns, matrices, and multidimensional data.
16. else with Loops
Python allows else with for and while loops.
for i in range(5): print(i) else: print("Loop completed")
Output:
0 1 2 3 4 Loop completed
The loop's else block executes when the loop finishes normally.
If the loop is stopped using break, the else block does not execute.
for i in range(5): if i == 3: break print(i) else: print("Completed")
Output:
0 1 2
17. Conditional Expression
Python also provides a short form of if-else.
Normal
age = 20 if age >= 18: result = "Adult" else: result = "Minor"
Short Form
age = 20 result = "Adult" if age >= 18 else "Minor" print(result)
Output:
Adult
This is called a conditional expression or ternary expression.
Control Flow Summary
| Statement | Purpose |
|---|---|
if | Checks a condition |
elif | Checks another condition |
else | Executes when conditions are false |
for | Repeats over an iterable |
while | Repeats while a condition is true |
break | Stops a loop |
continue | Skips current iteration |
pass | Does nothing |
range() | Generates a sequence of numbers |
Simple Structure
Control Flow │ ├── Conditional Statements │ ├── if │ ├── if-else │ ├── if-elif-else │ └── Nested if │ ├── Loops │ ├── for │ └── while │ └── Loop Control ├── break ├── continue └── pass
In simple words: Control flow allows a Python program to make decisions and repeat or control the execution of code.