0% found this document useful (0 votes)
13 views4 pages

Continue, Break, Pass

The document explains the use of the Python keywords break, continue, and pass for controlling loop execution. The break keyword exits a loop prematurely, continue skips the current iteration, and pass serves as a placeholder when no action is needed. Examples are provided for each keyword to illustrate their functionality.

Uploaded by

nazish.z.nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Continue, Break, Pass

The document explains the use of the Python keywords break, continue, and pass for controlling loop execution. The break keyword exits a loop prematurely, continue skips the current iteration, and pass serves as a placeholder when no action is needed. Examples are provided for each keyword to illustrate their functionality.

Uploaded by

nazish.z.nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Keywords: break, continue, and 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.

You might also like