Table of contents | |
Introduction | |
The Break Statement | |
The Continue Statement | |
The Pass Statement | |
Sample Problems |
When writing programs, it's important to have control over the flow of execution. Python provides three statements, namely break, continue, and pass, which allow you to manipulate the flow of your code. In this article, we will explore each of these statements, understand their purpose, and see how they can be used in practical examples.
The break statement is used to terminate the execution of a loop prematurely. It can be used with both for and while loops. When a break statement is encountered, the loop immediately ends, and the program continues with the next statement after the loop.
Let's consider an example of a while loop that counts down from 5 to 1 and breaks when it reaches 3:
count = 5
while count > 0:
print(count)
if count == 3:
break
count -= 1
Output:
5
4
3
Explanation:
In this code, the while loop runs as long as the variable count is greater than 0. Inside the loop, the current value of count is printed. If the value of count is equal to 3, the break statement is encountered, and the loop is terminated immediately.
The continue statement is used to skip the rest of the code inside a loop for the current iteration and move to the next iteration. This statement is often used to avoid executing certain code under specific conditions.
Let's consider an example of a for loop that prints all odd numbers from 1 to 10, skipping the number 5 using the continue statement:
for num in range(1, 11):
if num == 5:
continue
if num % 2 == 1:
print(num)
Output:
1
3
7
9
Explanation:
In this code, the for loop iterates over the numbers from 1 to 10. If the current number is equal to 5, the continue statement is encountered, and the remaining code inside the loop is skipped for that iteration. As a result, the number 5 is not printed. For all other odd numbers, the print statement is executed.
The pass statement is a placeholder statement in Python that does nothing. It is often used as a syntactic requirement when a statement is needed for the code's structure, but no action is required. It is commonly used as a placeholder for future code or as a temporary placeholder when writing functions or classes.
Let's consider an example where we define a function that is not yet implemented using the pass statement:
def calculate_average(numbers):
pass
Explanation:
In this code, we define a function called calculate_average that is not implemented yet. Instead of leaving the function empty, we use the pass statement as a placeholder. This allows us to define the function without causing any syntax errors. Later on, we can replace the pass statement with the actual code for calculating the average.
Problems 1: Write a program that prompts the user for a number between 1 and 10. If the number is divisible by 3, print "Divisible by 3" and exit the program using the break statement. Otherwise, continue prompting for a number until a valid input is provided.
while True:
num = int(input("Enter a number between 1 and 10: "))
if num % 3 == 0:
print("Divisible by 3")
break
else:
print("Invalid input. Try again.")
Problems 2: Write a program that prints all even numbers between 1 and 20, excluding multiples of 5 using the continue statement.
for num in range(1, 21):
if num % 5 == 0:
continue
if num % 2 == 0:
print(num)
Understanding control flow statements like break, continue, and pass is essential for writing efficient and structured code in Python. The break statement allows you to exit a loop prematurely, the continue statement lets you skip certain code within a loop, and the pass statement is used as a placeholder. By mastering these control flow statements, you can have more control over the execution flow of your programs and handle different scenarios effectively.
49 videos|38 docs|18 tests
|
|
Explore Courses for EmSAT Achieve exam
|