Jump Statements in Python
Jump statements are used to control the flow of a loop or a program. Python provides three main jump statements: break
, continue
, and pass
.
1. The break
Statement
The break
statement is used to exit a loop prematurely, regardless of the loop’s condition. It is often used when a specific condition is met, and further execution of the loop is unnecessary.
Syntax:
break
Example: Breaking a Loop
for number in range(1, 10):
if number == 5:
print("Breaking the loop at", number)
break
print("Number:", number)
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Breaking the loop at 5
2. The continue
Statement
The continue
statement skips the rest of the code in the current iteration and moves to the next iteration of the loop.
Syntax:
continue
Example: Skipping an Iteration
for number in range(1, 10):
if number % 2 == 0:
continue # Skip even numbers
print("Odd number:", number)
Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9
3. The pass
Statement
The pass
statement does nothing and is used as a placeholder. It allows you to write code where a statement is syntactically required but no action is needed.
Syntax:
pass
Example: Placeholder for Future Code
for number in range(1, 5):
if number == 3:
pass # Placeholder for future logic
else:
print("Processing number:", number)
Output:
Processing number: 1
Processing number: 2
Processing number: 4
Comparison of Jump Statements
Statement | Purpose | Example |
---|---|---|
break | Exits the loop entirely. | Exit when x == 5 . |
continue | Skips the rest of the current iteration. | Skip even numbers in a loop. |
pass | Does nothing; acts as a placeholder. | Placeholder for future code. |
Advanced Examples
Example 1: Find the First Even Number
Using break
to find the first even number in a list:
numbers = [3, 5, 7, 8, 9]
for num in numbers:
if num % 2 == 0:
print("First even number is:", num)
break
Output:
First even number is: 8
Example 2: Skip Non-Alphabet Characters
Using continue
to skip non-alphabet characters in a string:
text = "Python123ForAll!"
for char in text:
if not char.isalpha():
continue
print(char, end=" ")
Output:
P y t h o n F o r A l l
Example 3: Using pass
in a Function
Using pass
to define an empty function:
def placeholder_function():
pass # Logic will be added later
# This function does nothing for now
Try It Yourself
Problem 1: Break When Number Is Found
Write a program to find a number in a list and exit the loop once it’s found. Print “Not found” if the number is not in the list.
Show Code
numbers = [10, 20, 30, 40, 50]
target = 30
for num in numbers:
if num == target:
print("Number found:", target)
break
else:
print("Number not found.")
Problem 2: Skip Negative Numbers
Write a program to calculate the sum of positive numbers in a list, skipping negative numbers.
Show Code
numbers = [5, -3, 8, -2, 7]
total = 0
for num in numbers:
if num < 0:
continue # Skip negative numbers
total += num
print("Sum of positive numbers:", total)
Problem 3: Pass in a Loop
Write a program to iterate over a list of tasks. If a task is marked as “Pending,” use pass
to skip processing it.
Show Code
tasks = ["Complete assignment", "Pending", "Submit project"]
for task in tasks:
if task == "Pending":
pass # Placeholder for future logic
else:
print("Processing task:", task)