CCD Module14-PL101 01
CCD Module14-PL101 01
In this lesson, we will learn to use break, continue and pass statements to alter the flow
of a loop.
References:
• Python Programming for Beginners
INFORMATION SHEET PL 101-14.1.1
“Python Break, Continue and Pass”
The break statement is used to terminate the loop immediately when it is encountered.
break
The working of break statement in for loop and while loop is shown above.
We can use the break statement with for loop to terminate the loop when a certain condition is met.
For example,
for i in range(5):
if i == 3:
break
print(i)
Output
0
1
2
In the above example, we have used the for loop to print the value of i. Notice the use of
if i == 3:
break
Here, when i is equal to 3, the break statement terminates the loop. Hence, the output doesn't
Note: The break statement is almost always used with decision-making statements.
We can also terminate a while loop using the break statement. For example,
# program to find first 5 multiples of 6
i=1
while (i<=10):
print('6 * ',(i), '=',6 * i)
if i >= 5:
break
i=i+1
Output
6* 1=6
6* 2 = 12
6* 3 = 18
6* 4 = 24
6* 5 = 30
In the above example, we have used the while loop to find the first 5 multiples of 6. Here notice the line,
if i >= 5:
break
This means when i is greater than or equal to 5, the while loop is terminated.
The continue statement is used to skip the current iteration of the loop and the control flow of the
continue
We can use the continue statement with the for loop to skip the current iteration of the loop.
Then the control of the program jumps to the next iteration. For example,
for i in range(5):
if i == 3:
continue
print(i)
Output
0
1
2
4
In the above example, we have used the for loop to print the value of i. Notice the use of
if i == 3:
continue
Here, when i is equal to 3, the continue statement is executed. Hence, the value 3 is not printed to the
output.
In Python, we can also skip the current iteration of the while loop using the continue statement.
For example,
num = 0
if (num % 2) == 0:
continue
print(num)
Output
1
3
5
7
9
In the above example, we have used the while loop to print the odd numbers between 1 to 10.
if (num % 2) == 0:
continue
Here, when the number is even, the continue statement skips the current iteration and starts
In Python programming, the pass statement is a null statement which can be used as a
Suppose we have a loop or a function that is not implemented yet, but we want to implement it
in the future. In such cases, we can use the pass statement.
pass
n = 10
Here, notice that we have used the pass statement inside the if statement.
However, nothing happens when the pass is executed. It results in no operation (NOP).
n = 10
if n > 10:
# write code later
print('Hello')
Note: The difference between a comment and a pass statement in Python is that while the
We can do the same thing in an empty function or class as well. For example,
def function(args):
pass
class Example:
pass
PRECAUTIONS:
Do not just copy all your output from the internet.
Use citation and credit to the owner if necessary.
ASSESSMENT METHOD: WRITTEN WORK CRITERIA CHECKLIST
STUDENT NAME: _____________________________ SECTION: __________________
CRITERIA SCORING
Did I . . .
1 2 3 4 5
1. Focus - The single controlling point made with an awareness of a
task about a specific topic.
2. Content - The presentation of ideas developed through facts,
examples, anecdotes, details, opinions, statistics, reasons, and/or
opinions
3. Organization – The order developed and sustained within and
across paragraphs using transitional devices and including the
introduction and conclusion.
4. Style – The choice, use, and arrangement of words and sentence
structures that create tone and voice.
5. .
6. .
7. .
8. .
9. .
10. .
TEACHER’S REMARKS: QUIZ RECITATION
PROJECT
GRADE:
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_______________________________
TEACHER
Date: ______________________