Introduction to Python Programming Cycle of Python Varibles and Datatypes in Python Input and Output in Python Operators in Python Precedence of Operators in Python Typecasting in Python If Else in Python Loops in Python Break, Continue and Pass in Python Functions in Python Default Arguments and Keyword Arguments in Python Strings in python Lists in Python Tuple in Python Dictionary in Python Sets in Python List Comprehension in Python Unpacking Sequences in Python Higher Order Functions in Python Lambda Functions in Python Sieve of Eratosthenes Algorithm in Python Linear Search in Python Binary Search in Python Selection Sort in Python Bubble Sort in Python Insertion Sort in Python Merge Sort in Python

Break, Continue and Pass in Python

Break and Continue are mainly used to control loops. Manytime we need to skip any specific iteration or end loop when some specific condition arises, here we use break and continue.
Jump to specific section:
  1. Break
  2. Continue
  3. pass

Break

Break keyword is used to break loops when any specific condition arises.
Example:

for i in range(10):
print(i)
if (i==5):
    print("Break loop if i = 5")
    break
    
Output:
0
1
2
3
4
5
Break loop if i = 5 

Continue

Continue keyword is used to skip some iterations based on conditions.
Example:

for i in range(10):
    if (i==5):
        print("When i = 5, code below continue will not execute for current iteration")
        continue
    print(i)
    
Output:
0
1
2
3
4
When i = 5, code below continue will not execute for current iteration
6
7
8
9

Pass

In python we can not leave any block empty. If we need to leave any block empty then we use 'pass' keyword.
Let us see what will happend if we leave block empty.

for i in range(10):
    # this block is left empty, this line 
    # is not considered as code 
    # because this is comment
Output:
File "", line 4
# because this is comment
^
IndentationError: expected an indented block after 'for' statement on line 1
Now we will use pass keyword

for i in range(10):
    # this block is left empty, this line 
    # is not considered as code 
    # because this is comment
    pass
Output:

Output is empty and no any error arise.