Python Break, Continue and Pass Statements

Python has provided with different loop control statements to control the flow of the loop.

Loading…

What are Loop Control statements in Python?

Python provides three loop control statements:

  • break
  • continue
  • pass

What is break loop control statement in Python?

The break statement is used to exit out of the loop. It stops the flow of execution of the current loop and passes to the next statements after the loop.

Syntax of break statement:-

#loop statements
break

Flowchart of break statement:-

Example of break statement:-

for i in range(0, 5):
    if i > 2:
        break
    print(i)

print("Body after loop")

The output of break statement:-

0
1
2
Body after loop
Here the break statement will break the loop when the value of i is greater than 2.

What is continue loop control statement in Python?

The continue statement is used to continue with the existing loop body and pass the control to the beginning of the body without executing the next statement in loop.

The syntax of continue statement:-

#loop statements
continue

Flowchart of continue statement:-

Example of continue statement:-

for i in range(0, 5):
    if i == 2:
        continue
    print(i)

print("Body after loop")

The output of continue statement:-

0
1
3
4
Body after loop
Here the continue statement will continue the execution and will not print the value of i if i is equal to 2.

What is pass loop control statement in Python?

The pass statement is used to execute nothing in Python. It is a null statement in Python. It is used as a placeholder. It can be used when we have a loop or function or class that have not yet been implemented. Let’s use the following examples to have an idea of pass statement.

for i in range(0, 5):
    pass

def sum(a, b):
    pass

class Vehicle:
    pass
Loading…