In Python, the break and continue statements determine the flow of program execution. These statement control when the program continues the process and when it breaks execution.

The break Statement is used to abruptly stop the flow of the program, hence forcing the program to break out of the continuous or infinite loop.
Let's see an example where we will use the break statement.
Output:
1
Explanation
In the above example, we used the break statement, which immediately stopped the flow of the program after the first iteration, and the output printed was only 1. It means as soon as the 1 is iterated, the break statement does its job.
The continue statement in Python is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration immediately.
We use the continue statement in some conditions where we want some situations to be completely ignored without affecting anything or breaking out of the entire loop. The Python continue statement in Python can be used in for and while loops to improve code efficiency and readability.
Let's take the same example to see what will happen if we use the continue statement.
Output:
1 3 5 7 9
Explanation
In the above example, the continue statement is executed for every number known as num, which causes the program to skip the print(num) function and move on to the next iteration.
Let's quickly recap the difference between break and continue, which will help us remember the key points.
| break Statement | continue Statement |
|---|---|
| The Break Statement in Python is used to abruptly stop the flow of the program. | The continue statement in Python is used to skip the rest of the code inside a loop for the current iteration. |
| It is commonly used inside switch blocks and is also valid in for, while, and do-while loops. | It is not applicable in switch statements but can be used in for, while, and do-while loops. |
| Once the break is executed, control moves directly outside the loop or switch structure. | When continue is executed, control jumps to the next cycle of the loop. |
| Syntax: break; | Syntax: continue; |
| It can be combined with labeled statements to exit outer loops in nested structures. | It does not support labeled usage for controlling outer loop flow. |
| Any remaining loop iterations are completely skipped after the break is triggered. | Remaining iterations still run, except the iteration in which continue appears. |
Break and Continue statement acts like a steering wheel that controls the flow of the program by deciding whether to continue the process or break it abruptly.
They both use the syntax of their name, such as break and continue, respectively, followed by the colon(;). We used examples and output with their explanation to understand the working of these statements in the program. Lastly, we recapped the tutorial by making the tabular differences of Break and Continue.
We request you to subscribe our newsletter for upcoming updates.