Page 402 - Computer_Science_F5
P. 402
(c) Control statements: break, continue, and pass
Python offers control statements that allow you to modify the flow of a loop according
to specific conditions.
The “break” statement is utilised to abruptly exit the loop before its normal Chapter Six: Object oriented programming with Python
FOR ONLINE READING ONLY
completion. It provides a means to terminate the loop prematurely based on a certain
condition.
The “continue” statement, on the other hand, is used to skip the remaining code
within a loop for the current iteration. It allows you to jump to the next iteration
without executing the subsequent statements in the loop.
Lastly, the “pass” statement is a placeholder that does nothing. It is used when
syntactically required but no specific action is needed. It is commonly used as a
temporary placeholder until the code is implemented. For example,
break :
# Search for a number in a list and stop when found
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13]
search_number = 11
for number in numbers:
if number == search_number:
print(“Number found!”)
break
else:
print(“Searching...”)
Continue:
# Print odd numbers from 1 to 13 skipping even numbers
for i in range(1, 14):
if i % 2 == 0:
continue
print(i)
Pass:
Here are a couple of examples showcasing the usage of the “pass” statement:
393
Student’s Book Form Five
Computer Science Form 5.indd 393 23/07/2024 12:34

