Python Flow Control

Python break and continue

Python loops are used for the continuation of a set of codes without the need of writing the same piece of program lines again and again. But there arises a point where we would like to end or exit the loop, skip a part of the iteration cycle or just ignore that condition. For this purpose, Python provides 2 built-in loop control statements to have a strong grip and control over the loops. They are Python Break statement and Python Continue statement. Let us learn about each one of them in detail in this article.

Why do we use Break or Continue Statements?

The loop control statements i.e. Python break and continue statements are used to interrupt the execution flow and terminate/skip the iterations as per the need. The Python break and continue are used within the loop to deviate from the loop’s regular procedure.

Python supports two forms of loops: “for” and “while.” A for-loop or while-loop is designed to iterate until the provided condition fails. When you employ a Python break or continue statement, the loop’s flow is altered from its typical condition. Python loop control statements can also be used within nested loops.

Python Break Statement

Definition

  • The Python break statement is used to terminate the current loop of the iteration and resume execution of the next following statements in the program.

During an iteration, if we do not want a condition to be performed, or if we want to exit the loop after a certain condition has been met and perform the next set of code, we use the Python Break statement. If the break statement is inside a nested loop, then the break statement will terminate the inner loop.

Syntax

Flowchart

Python Break statement flowchart

Let us consider a basic example to understand the concept of Python break statements.

Example 1

                    

# using break statement in for loop
num = [1, 2, 3, 4, 5, 6 ,7, 8]

for n in num:
    if n == 4:
        print('Break statement executed for n = 4')
        break
    else:
        print(n)
print('End of for loop')

Output

                    

1
2
3
Break statement executed for n = 4
End of for loop

Explanation

In this program, we iterate through the list ‘num’. In the for loop, we check if the value of n is equal to 4. If it equals 4 then the break statement is used to end the loop and perform the further set of code. Hence, we see in the output that all the numbers up to 4 are printed and after that, the loop terminates.

Another example of break statement in while loop is,

Example 2

                    

# using break in while loop
num = [1, 2, 3, 4, 5, 6 ,7, 8]
n = 0

while True:
    if num[n] == 6:
        print('break statement executed')
        break
    else:
        print(num[n])
        n = n + 1
print('End of while loop')

Output

                    

1
2
3
4
5
break statement executed
End of while loop

Python Continue Statement

Definition

  • The Python continue statement is used to skip the next part of the code in the iteration and restart with the next iteration of the loop.

In an iteration, if we want to skip a certain iteration based on the condition specified and then carry on with the further code, we make use of Python’s continue statement.

Syntax

Flowchart

Python Continue statement flowchart

Let us understand the continue loop control statement by looking at the following example.

Example

                    

# using continue in while loop
n = 10

while n > 0:
    n = n - 1
    if n%2 == 0:
        continue
    print('Odd Numbers:', n)
print("Loop is finished")

Output

                    

Odd Numbers: 9
Odd Numbers: 7
Odd Numbers: 5
Odd Numbers: 3
Odd Numbers: 1
Loop is finished

In this program, we check if the modulus of the data element, when divided by 2, is equal to 0. If it is equal to zero, we ignore that iteration by using the continue statement. We only print the values of Odd numbers in this program. Therefore you can see that all the even numbers are neglected by the continue loop control statement and the other remaining values are printed.

Using Break and Continue in a Single Program

Let us understand the basic difference between the break and continue statement by using it in a single program.

Example

                    

students = ['Ashton', 'Jack', 'Rose', 'Tim', 'Elle', 'Johnny', 'Sammy', 'David', 'Monica', 'Arjun']

for n in students:
    if len(n) == 4:  # values whose lengths are 4 will be ignored and others will be printed
        continue
    print('Hello', n)

    if n == 'David':  # values after David will not be printed because of break statement
        break
print('Loop ended!')

Output

                    

Hello Ashton
Hello Tim
Hello Johnny
Hello Sammy
Hello David
Loop ended!

Break continue

If the condition before the continue statement is satisfied, the flow of the program is passed on to the above iteration. If the condition for the break statement is satisfied, then the iteration is immediately ended then and there itself and the flow of the program is passed on to the next set of codes written below.

Frequently Asked Questions

Q1. What is a break statement in Python?

Break statement in Python is a loop control statement which is used to terminate any iteration if the condition has been satisfied. Break statements are used if we want to immediately stop an iteration or would like to end the loop at a sudden point.

For example, Let’s imagine you want the software to terminate when you get your number completely divided by six. If you use a break statement, your program flow will break and jump to the next body of programming, or it will exit the program if there is nothing left to do.

                    

for number in range(1,10):
    if number%6 == 0:
        break
    print(number)

Output

Q2. Is it bad to use break in Python?

‘Break’ statements can be found in a wide range of professional code. It makes perfect sense to use this whenever possible. It enhances readability and simplifies the code. There’s nothing wrong with utilizing a break statement, but nested loops can be difficult to understand. When you write neat, easily-readable loops, most of the “dangers” connected with using break or continue in a for loop are neutralized. Yes, you may easily forget that some code won’t be executed after the break if the body of your loop extends several screen widths and has multiple nested sub-blocks. The purpose of the break statement should be evident if the loop is short and to the point.

Q3. How do you use continue in Python?

We use the continue statement in order to skip some part of the iteration based on the specified condition. In layman terms, the control is transferred back to the start for the following iteration after the continue statement skips the code that follows it.

Example

                    

for number in range(1,8):
    if number == 5:
       continue
    else:
       print(number)

Output

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.