Introduction: Python break and continue
The break and continue statements are control flow statements in Python that allow you to modify the behaviour of loops. They provide additional control and flexibility in deciding when to terminate a loop or skip certain iterations. In this article, we will explore the concepts of break and continue in Python, understand their differences, and provide examples to illustrate their usage.
The break Statement:
The break statement is used to exit a loop prematurely, even if the loop condition is still true. When the break statement is encountered inside a loop, the loop is immediately terminated, and the program execution continues to the next statement outside the loop. It is commonly used to stop the execution of a loop when a specific condition is met.
Example 1: Using break in a while loop
count = 1
while True:
print(count)
count += 1
if count > 5:
break
Output:
1
2
3
4
5
In this example, the while loop continues indefinitely. However, when the value of “count” becomes greater than 5, the break statement is executed, causing the loop to terminate. As a result, the numbers 1 to 5 are printed, and the program execution moves on.
Example 2: Using break in a for loop
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
if fruit == 'banana':
break
print(fruit)
Output:
apple
In this example, the for loop iterates over a list of fruits. When the loop encounters the string ‘banana’, the break statement is executed, and the loop is abruptly terminated. As a result, only the first fruit, ‘apple’, is printed.
The continue Statement: The continue statement is used to skip the rest of the code block for the current iteration of a loop and move on to the next iteration. It allows you to control which iterations should be executed and which should be skipped.
Example 1: Using continue in a while loop
count = 1
while count <= 5:
if count == 3:
count += 1
continue
print(count)
count += 1
Output:
1
2
4
5
In this example, the while loop iterates from 1 to 5. However, when the value of “count” is equal to 3, the continue statement is executed. This causes the rest of the code block to be skipped for that iteration, and the loop proceeds to the next iteration. As a result, the number 3 is not printed.
Example 2: Using continue in a for loop
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
if fruit == 'banana':
continue
print(fruit)
Output:
apple
orange
In this example, the for loop iterates over a list of fruits. When the loop encounters the string ‘banana’, the continue statement is executed. This skips the remaining code block for that iteration and moves on to the next iteration. As a result, ‘banana’ is not printed.
Key Differences between break and continue:
- The break statement terminates the entire loop and proceeds to the next statement outside the loop. The continue statement skips the remaining code for the current iteration and moves to the next iteration of the loop.
- Break is used to exit the loop entirely, while continue is used to skip a specific iteration.
- Break can be used in both while and for loops, whereas continue is only applicable in loop structures.
The break and continue statements are powerful control flow tools in Python that allow you to modify the behavior of loops. The break statement is used to exit a loop prematurely, while the continue statement skips the remaining code for the current iteration. Understanding when and how to use break and continue will help you write more efficient and concise code, enabling you to have greater control over the flow of your program.