Introduction to Control Flow in Python:
Most programming languages, including Python, execute code sequentially from the top of the source file to the bottom, line by line. This way of running code is logical and similar to following a series of instructions step by step.
In programming, control flow refers to the order in which statements are executed inside a program.
Normally, Python follows a sequential flow. However, this flow can be changed using control flow statements such as:
Conditional Statements
Loops
Loop Control Statements
These statements help programs make decisions and repeat actions efficiently.
Conditional Statements
A conditional statement allows a program to execute specific blocks of code only when a particular condition is true.
If the condition is false, the code block is skipped.
Conditional statements make programs interactive and decision-based instead of simply running line by line.
Using if Statement
The simplest conditional statement in Python is the if statement.
Syntax
Python
if condition:
# code block
If the condition is True, the indented block executes.
If the condition is False, Python skips that block.
Example
Python
order_total = 215.00
if order_total >= 150:
print("You got free shipping!")
Output
Python
You got free shipping!
Using if...else Statement
The if...else statement provides an alternative block of code when the condition becomes false.
Syntax
Python
if condition:
# code block
else:
# alternative block
Example
Python
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Output
Python
x is less than or equal to 5
Using if...elif...else Statement
The if...elif...else statement is used to check multiple conditions one after another.
Python evaluates each condition sequentially:
If one condition becomes true, its block executes.
Remaining conditions are skipped.
The else block runs only when all conditions are false.
Syntax
Python
if condition1:
# code block
elif condition2:
# code block
else:
# code block
Example
Python
x = 5
if x < 5:
print("x is less than 5")
elif x > 5:
print("x is greater than 5")
else:
print("x is equal to 5")
Output
Python
x is equal to 5
Nested Conditional Statements
Sometimes, multiple conditions need to be checked together.
This can be done using nested conditionals.
Example
Python
number = 7
if number > 0:
if number < 10:
print("The number is between 0 and 10!")
Output
Python
The number is between 0 and 10!
Here:
First condition checks whether the number is greater than 0.
Second condition checks whether it is less than 10.
Since both conditions are true, the message is printed.
Using Boolean Operators
Instead of nesting multiple conditions, Boolean operators often provide a cleaner solution.
Example
Python
number = 7
if number > 0 and number < 10:
print("The number is between 0 and 10!")
Output
Python
The number is between 0 and 10!
Loops in Python
Loops are used to execute a block of code repeatedly.
Python mainly supports:
for loop
while loop
for Loop
A for loop is used to iterate through a sequence such as a list, tuple, string, or range.
Example
Python
a = [1, 2, 3]
for i in a:
print(i)
Output
Python
1
2
3
while Loop
A while loop repeats a block of code as long as the condition remains true.
Syntax
Python
while condition:
# code block
Example
Python
count = 1
while count <= 5:
print(count)
count += 1
Output
Python
1
2
3
4
5
Loop Control Statements
Python provides special statements to control loop execution efficiently.
These are:
break
continue
pass
break Statement
The break statement immediately exits the loop when a condition is met.
Example
Python
for i in range(10):
if i == 5:
break
print(i)
Output
Python
0
1
2
3
4
continue Statement
The continue statement skips the current iteration and moves to the next iteration.
Example
Python
for i in range(10):
if i % 2 == 0:
continue
print(i)
Output
Python
1
3
5
7
9
pass Statement
The pass statement does nothing.
It acts as a placeholder for future code.
Example
Python
for i in range(5):
if i == 3:
pass
print(i)
Output
Python
0
1
2
3
4
Conclusion
Control flow statements are essential in Python because they determine how a program executes.
Conditional statements help programs make decisions, while loops allow repetitive execution of tasks.
By combining:
if
if...else
if...elif...else
for
while
break
continue
pass
developers can create efficient and dynamic Python programs.
Reference:
https://realpython.com/python-control-flow/#getting-to-know-control-flow-in-python
https://www.tpointtech.com/control-statements-in-python
https://www.geeksforgeeks.org/dsa/loops-and-control-statements-continue-break-and-pass-in-python/
Top comments (0)