Break, Pass, and Continue Statement in Python

Break, Pass and Continue Statement in Python

Hello Techies,

In this tutorial, we will learn about What is break and continue statement in Python as well as pass statements in Python with the help of examples.

Python Break Statement

The break statement is used to exit from the loop to execute the next statement in the program.

You can use break statements in while as well as in for loops.

The break statement in Python is used to bring the control out of the loop if any external condition arises.

Syntax of break statement in Python

break

Flowchart of break statement

Flowchart of break statement

How to use Break statement in while loop in Python

while condition:
    if(condition):
        break
Rest of the code ...

How to use Break statement in for loop in Python

for condition:
    if(condition):
        break
Rest of the code ...

Example of Break in Python

for i in range(10):
    print(i)
    if (i == 5):
        break
print("Rest of the code")

Output:

0
1
2
3
4
5
Rest of the code

Python Pass Statement

The pass statement is used to do nothing. This can be used in a loop or in an if statement to represent no operation. The pass is useful when we need statement synthetically correct but we do not want to perform any operation.

Syntax of Pass statement in Python

pass

Example of Python pass Statement

for i in range(10):
   if i == 5:
      pass
      print('This is pass block')
   print(i)

print("Rest of the code")

Output:

0
1
2
3
4
This is pass block
5
6
7
8
9
Rest of the code

Continue Statement in Python

The Continue statement is used in a loop to return to the beginning of the loop. You can use break statements in while as well as in for loops.

Syntax of Continue statement in Python

continue

Flowchart of continue statement

Flowchart of continue statement

How to use Break statement in continue loop in Python

while condition:
    if(condition):
        continue
Rest of the code ...

How to use Break statement in for loop in Python

for condition:
    if(condition):
        continue
Rest of the code ...

Example of Python continue

for i in range(10):
    if (i == 5):
        continue
    print(i)
print("Rest of the code")

Output:

0
1
2
3
4
6
7
8
9
Rest of the code
Interview Question

Q    : What is the difference between break and continue in Python?
Ans: The main difference between break and continue statements is that when
         the break keyword arrives, it will come out of the loop. In case of Continue
         keywords, the current iteration will be stopped and will continue with the
         next iteration.

Conclusion

In this tutorial, you learned about Break, Pass, and Continue Statement in Python, We have covered the following points:

  • Break statement in Python (Syntax, Flowchart with example)
  • Pass statement in Python (Syntax, Flowchart with example)
  • Continue statement in Python (Syntax, Flowchart with example)
  • Interview question on Break and Continue statements.

Leave a Comment