Control Flow
Definition: Control flow is the order in which individual statements, instructions, or function calls are executed or evaluated in a program. Python provides control flow tools like conditional statements, loops, and control statements to manage execution paths.
Control Flow
-
Conditional Statements
- if
- if โ else
- if-elif-else
- nested-if-else
-
Iterative Statements
- for loop
- while loop
-
Control Statements
- break
- continue
- pass
โ Conditional Statements
Used to make decisions based on conditions.
1. if Statement
Definition: Executes a block of code if a specified condition is true.
if condition:
# code block
x = 10
if x > 5:
print("x is greater than 5")
>2. if-else Statement
Definition: Executes one block if condition is true, another if false.
if condition:
# true block
else:
# false block
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
3. if-elif-else Statement
Definition: Multiple conditions can be tested in order.
if condition1:
# block 1
elif condition2:
# block 2
else:
# default block
x = 7
if x > 10:
print("x > 10")
elif x == 7:
print("x is 7")
else:
print("x is something else")
4. Nested if-else Statement
Definition: An if or else block can contain another if-else.
if condition1:
if condition2:
# nested block
else:
# nested else
else:
# outer else
x = 10
if x > 5:
if x < 15:
print("x is between 5 and 15")
else:
print("x is 15 or more")
else:
print("x is 5 or less")
๐ Iterative Statements (Loops)
Used to repeat code execution.
1. for Loop
Definition: Iterates over a sequence (like list, string, or range).
for item in sequence:
# loop block
for i in range(5):
print(i)
2. while Loop
Definition: Repeats as long as the condition is true.
while condition:
# loop block
x = 0
while x < 5:
print(x)
x += 1
โ Control Statements
Used to control loop execution.
1. break
Definition: Exits the loop entirely.
for i in range(10):
if i == 5:
break
print(i)
2. continue
Definition: Skips the rest of the loop and moves to the next iteration.
for i in range(5):
if i == 2:
continue
print(i)
3. pass
Definition: Placeholder; does nothing. Useful when the syntax requires a block.
for i in range(3):
pass # Placeholder for future code
๐งช Practical Examples
โ Example 1: Check Even or Odd
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
๐ Example 2: for Loop Through a List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
โ Example 3: Stop on Negative Number
numbers = [1, 3, -1, 5]
for num in numbers:
if num < 0:
break
print(num)
๐ Example 4: While Loop with Control
x = 0
while x < 5:
if x == 3:
x += 1
continue
print(x)
x += 1
โ Summary Table: Python Control Structures
Control Structure | Description | Key Use Case |
---|---|---|
if, elif, else |
Executes code blocks based on specified conditions. | Implementing decision-making logic. |
for loop |
Iterates over a sequence (e.g., list, range, string). | Looping through known sequences or collections. |
while loop |
Repeats code while a condition remains true. | When the number of iterations is unknown. |
break |
Immediately exits the nearest enclosing loop. | Stopping a loop early when a condition is met. |
continue |
Skips the current iteration and moves to the next. | Skipping over specific iterations in a loop. |
pass |
Does nothing; used as a placeholder for future code. | Defining empty code blocks or stubs. |