Python Program Flow Control
Loop Manipulation using pass, continue, break, and else
Pass: The pass statement is a placeholder that does nothing. It is often used when a
statement is syntactically required, but you don't want to execute any code at that point.
Syntax of Pass :
if condition:
pass # Do nothing
Example:
for i in range(5):
if i == 3:
pass # Do nothing when i is 3
else:
print(i)
Output:
Continue: The continue statement is used to skip the current iteration of the loop and move
to the next iteration. It stops the current iteration and proceeds with the next cycle.
Syntax of Pass :
for item in iterable:
if condition:
continue # Skip the rest of the code and move to the next iteration
Example:
for i in range(5):
if i == 3:
continue # Skip when i is 3
print(i)
Output:
Break: The break statement is used to exit the loop entirely, regardless of the condition. It is
commonly used when a specific condition is met and you no longer want to continue the loop.
Syntax of Pass :
for item in iterable:
if condition:
break # Exit the loop
Example:
for i in range(5):
if i == 3:
break # Stop the loop when i is 3
print(i)
Output:
2
else: The else block in loops runs only if the loop completes normally (i.e., it doesn’t exit early
using break). If the loop is terminated by break, the else block is skipped.
Syntax of Pass :
for item in iterable:
# Loop code
else:
# Code to run if loop completes without break
Example:
for i in range(3):
print(i)
else:
print("Loop finished without break")
Output:
Loop finished without break
#Differences
Pass: A placeholder for when you need a statement but don’t want any action.
Continue: Skips the current iteration and moves to the next one.
Break: Exits the loop entirely.
Else: Executes code after the loop finishes if it didn’t exit with break.
Programming using Python conditional and
loops blocks
Python conditional statements (if, elif, else) allow you to execute specific blocks of code based
on certain conditions. These can be combined with loops for powerful control over your
program flow.
Conditional Statements: Conditional statements are used to check whether an expression
evaluates to True or False and execute different blocks of code based on that.
Syntax of Pass :
if condition:
# Code block if condition is true
elif another_condition:
# Code block if the second condition is true
else:
# Code block if all conditions are false
Example 1:
age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
elif age < 60:
print("You are an adult.")
else:
print("You are a senior.")
Output:( Output Depends on the user Input)
Example 2:
for i in range(1, 11):
if i % 2 == 0:
print(f"{i} is even")
else:
print(f"{i} is odd")
Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even