Break And Continue Statements In Python Programming Language

Break And Continue Statements In Python Programming Language

In this article, you will learn how to use Break and Continue statements in Python programming language and how to use them in your code for different operations.

I have divided this article into easy to follow step by step sections so you can learn Break and Continue statements in python programming language easily.

Use of Break and Continue Statements in Python Programming Language

As you know that a loop is used to make a repeated statement flow in any program or make iterations until the specified condition meets the loop continues running.

But there is a way to alter the loop operations and that is with the use of Break and Continue statements in Python programming language.

Any kind of loop continues to repeat itself until the given test condition becomes false.

In case if we want to alter or stop the loop or even end the loop in the middle of its operation this is where we use Python break and continue statement.

Now I will explain each of them in detail so you can learn Break and Continue statements in Python programming language easily.

What is Python Break Statement?

Python break statement works in a way as if there is a loop running it ends the current execution of the loop and transfers the control of the program to the next statement of code.

If somewhere in code you are using nested loop which means loop inside another loop then the break statement terminates the most inner loop of the program.

Syntax of Python Break Statement


Flowchart of Python Break Statement


Practical Example of Python Break Statement

Example 1:

# First Example

for letters in "WalkIntoPC":
    if letters == "I":

print("Happy Codding")


Happy Codding!

In this example we run for loop over a string namely as “WalkIntoPC” and when loop encounters letter “I” the break statement breaks the loop.

Example 2:

# Second Example Of Break Statement

variable = 15                    
while variable > 0:              
   variable = variable -1
   if variable == 7:
   print ("Current variable value :", variable)
print ("Happy Codding!")


Current variable value : 14                                                                                                     
Current variable value : 13                                                                                                     
Current variable value : 12                                                                                                     
Current variable value : 11                                                                                                     
Current variable value : 10                                                                                                     
Current variable value : 9                                                                                                      
Current variable value : 8                                                                                                      
Happy Codding!

In this example, we set a variable to integer 15 and set the break statement as if digit 7 encounters the loop ends immediately.

What is Python Continue Statement?

The Python break statement terminates the loop whereas Python continue statement just skip the current iteration of a loop and the remaining loop body runs the same way.  Continue statement after skipping the current iteration hands over the control back to the beginning of the loop and the rest of the loop flows as normal.

Syntax of Python Continue Statement


Flowchart of Python Continue Statement


Practical Example of Python Continue Statement

Example 1:

# Program to show continue statement in a program

for value in "WalkIntoPC":
    if value == "I":

print("Happy Codding!")


Happy Codding!

In this example, we set a loop on a string to print the characters sequentially but we set the condition that when a character “I” encounters the continue statement skip that iteration of a loop and shift the control of loop to the beginning of the loop.

Jack Sparrow

Hay I Am Admin Of This Website. I Am Very Passionate About Computers And Technology. This Website Is The Result Of My Passion. I Love To Eliminate The Barriers That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

This Post Has 2 Comments

  1. Ian Willis

    I have totally understood the Break And Continue Statements. thanks for your hard work.

  2. Murtaza

    so nice and easy to do.

Leave a Reply