Continue, Break, Pass
Continue, Break, Pass
Introduction
In Python, the break, continue, and pass keywords are used to control the
flow of execution in loops and conditional statements. These keywords provide
a way to exit loops, skip iterations, or do nothing in specific situations.
1 Break
The break keyword is used to exit a loop prematurely. When break is encoun-
tered inside a loop (such as for or while), the loop is immediately terminated,
and the program continues with the next statement after the loop.
Syntax
break
Examples
# Example 1 : Using b r e a k i n a w h i l e l o o p
i = 0
while i < 5 :
i f i == 3 :
break # E x i t s t h e l o o p when i e q u a l s 3
print ( i )
i += 1
Output:
0
1
2
# Example 2 : Using b r e a k i n a f o r l o o p
f o r i in range ( 5 ) :
i f i == 3 :
break # E x i t s t h e l o o p when i e q u a l s 3
print ( i )
1
Output:
0
1
2
2 Continue
The continue keyword is used to skip the current iteration of a loop and proceed
to the next iteration. This can be useful when you want to skip specific values
or conditions without terminating the entire loop.
Syntax
continue
Examples
# Example 1 : Using c o n t i n u e i n a w h i l e l o o p
i = 0
while i < 5 :
i += 1
i f i == 3 :
continue # S k i p s t h e i t e r a t i o n when i e q u a l s 3
print ( i )
Output:
1
2
4
5
# Example 2 : Using c o n t i n u e i n a f o r l o o p
f o r i in range ( 5 ) :
i f i == 3 :
continue # S k i p s t h e i t e r a t i o n when i e q u a l s 3
print ( i )
Output:
0
1
2
4
2
3 Pass
The pass keyword is a placeholder used when a statement is syntactically re-
quired, but no action is needed. It is commonly used in situations where code
is being developed, but the implementation is not yet complete.
Syntax
pass
Examples
# Example 1 : Using p a s s i n a w h i l e l o o p
i = 0
while i < 5 :
i f i == 3 :
pass # P l a c e h o l d e r f o r f u t u r e code
else :
print ( i )
i += 1
Output:
0
1
2
4
# Example 2 : Using p a s s i n a f o r l o o p
f o r i in range ( 5 ) :
i f i == 3 :
pass # P l a c e h o l d e r f o r f u t u r e code
else :
print ( i )
Output:
0
1
2
4
Conclusion
The break keyword is used to exit a loop prematurely.
The continue keyword skips the current iteration of a loop and moves to
the next iteration.
3
The pass keyword does nothing and is often used as a placeholder for
future code.