Break, Continue and Pass in Python


Continue vs Break

Continue

print("The loop will skip the value when value of i is 2 and restart from next value of i - ")
for i in range(0,4):
    if i == 2:
        continue
    else:
        print(i+100)
Output:
The loop will skip the value when value of i is 2 and restart from next value of i - 
100
101
103

Break

print("The loop will break when value of i is 2. No more further execution! - ")
for i in range(0,4):
    if i == 2:
        break
    else:
        print(i+100)
Output:
The loop will break when value of i is 2. No more further execution! - 
100
101

Continue vs Pass

Continue

print('When value of i is 2, it will start from next iteration - ')
for i in range(0,4):
    if i==2:
        continue
    else:
        print(100+i)
    print(i)
Output:
When value of i is 2, it will start from next iteration - 
100
0
101
1
103
3

Pass

print('When value of i is 2, it does nothing and passes execution to next statement - ')
for i in range(0,4):
    if i==2:
        pass
    else:
        print(100+i)
    print(i)
Output:
When value of i is 2, it does nothing and passes execution to next statement - 
100
0
101
1
2
103
3

Jupyter Notebook Link - Break, Continue and Pass in Python{:target="_blank"}


Related Posts

Business Days with Custom Holidays

May 30, 2019

Read More
Months between two dates

May 30, 2019

Read More
Applying functions over pandas dataframe using apply, applymap and map

April 23, 2019

Read More