Break, Continue, and Pass Statements in Python

Break, Continue, and Pass Statements in Python

Last updated on 21st Sep 2020, Artciles, Blog

About author

Kamesh (Sr Technical Project Manager - Python )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 16757 Ratings 453

Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Subscribe For Free Demo

Error: Contact form not found.

  • Break statement
  • Continue statement
  • Pass statement

Break statement

The break statement is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available. If the break statement is present in the nested loop, then it terminates only those loops which contain a break statement.

Syntax:

break

Example:

  • number = 0

    for number in range(10):

        if number == 5:

            break    # break here

        print(‘Number is ‘ + str(number))

    print(‘Out of loop’)

Output

Number is 0

Number is 1

Number is 2

Number is 3

Number is 4

Out of loop

Continue statement

Continue is also a loop control statement just like the break statement. continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.

As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Syntax:

continue

Example:

  • for number in range(10):

        if number == 5:

            continue    # continue here

        print(‘Number is ‘ + str(number))

    print(‘Out of loop’)

Output

Number is 0

Number is 1

Number is 2

Number is 3

Number is 4

Number is 6

Number is 7

Number is 8

Number is 9

Out of loop

Pass statement

              As the name suggests, a pass statement simply does nothing. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is like null operation, as nothing will happen if it is executed. Pass statements can also be used for writing empty loops. Pass is also used for empty control statements, functions and classes.

Syntax:

pass

Example:

  • number = 0

    for number in range(10):

        if number == 5:

            pass    # pass here

        print(‘Number is ‘ + str(number))

    print(‘Out of loop’)

Output

Number is 0

Number is 1

Number is 2

Number is 3

Number is 4

Number is 5

Number is 6

Number is 7

Number is 8

Number is 9

Out of loop

Python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Conclusion

The break, continue, and pass statements in Python will allow you to use for loops and while loops more effectively in your code.

Are you looking training with Right Jobs?

Contact Us

Popular Courses