What is the Difference Between Break and Continue in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

In Python, a flow of a regular loop can be changed using the break and continue statement. Loops repeatedly run through the code block till the test expression returns true, but there are times when we want to stop the current iteration or even the entire loop without running the test expression. In these situations, the break and continue statements in Python are implemented.

Break Statement

A break statement is used to terminate the loop whenever a particular condition is satisfied. The statement is there just next after the loop receives control of the program. The break statement will end the innermost loop if it is contained within a nested loop that is the loop inside the other loop. It is used to end the loop that it is enclosed in, such as a do-while, while, switch, and for statement.

Syntax

Example 1

The example given below will give a better understanding of the break statement in Python.

Output

In the above example, we are printing the characters of the string "Aditya" until the character "t" is not encountered. At the moment when "t" is encountered the "if" condition will satisfy what is needed for the execution of the break statement. After execution of the break statement, the control of the program will directly jump to line number 7 which is just the next statement outside the loop.

Example 2

Output

From the above example, the concept of a break statement within a nested loop will get more clear. Here the inside for loop is breaking each time whenever "b" becomes equal to 3 and the control goes outside of that inside nested for loop, at last, the outside break statement will execute when "a" becomes equal to 3 and after that, the main for loop will also terminate which will lead to the end of the program.

Continue Statement

The continue statement skips the remaining lines of code, for the current iteration of the loop. In this case, the loop does not end, it continues with the next iteration.

Syntax

Example 1

The example given below will give a better understanding of the continue statement in Python.

Output

This example is similar to the above example of the break statement, the difference here is that the break statement is replaced with the continue statement. So here we are printing the characters of the string "Aditya" but when the character "t" is encountered then it skips that character and jumps to the next iteration.

Example 2

Output

From the above example, the concept of a continue statement within nested for loop will get clear. Here you can observe that whenever the "b" becomes equal to 2 then the continue statement is executing and skipping the current iteration of the inside loop.

Difference Between Break and Continue in Python

Basis for comparisonBreakContinue
UseIt is used for the termination of all the remaining iterations of the loop.It is used for the termination of the only current iteration of the loop.
Control after using break/continue statementThe line which is just after the loop will gain control of the program.The control will pass to the next iteration of that current loop by skipping the current iteration.
CausesIt performs the termination of the loop.It performs early execution of the next loop by skipping the current one.
ContinuationIt stops the continuation of the loop.It stops the execution of the current iteration.
OtherIt can be used with labels and switches.It can't be used with labels and switches.

Conclusion

  • The normal loop's flow can be changed by the use of the break and continue statement.
  • Break statement will end up in the innermost loop if it is used within a nested loop.
  • While using the continue statement the loops do not terminate but continuously go on with the next iteration.
  • Break and continue are the loop control statements as they are capable of altering the functioning of the loop.

Learn More: