
Loop Control Statements
In the previous articles, we've explored both the for
loop and the while
loop, understanding how they iterate through code. In this article, we'll learn loop control statements which allow us to tailor the behavior of loops to suit our specific needs.
There are three main loop control statements in Python: break, continue, and pass.
break: Terminates the loop prematurely.
continue: Skips the rest of the current iteration and continues with the next one.
pass: The pass statement is used as a placeholder in Python to avoid syntactical errors.
Using break
to Exit a Loop
Use break
when you want to exit a loop prematurely based on a certain condition.
Example
You are searching for a specific item in a list, and once you find it, you can exit the loop immediately.
# Create a listnumbers = [1, 2, 3, 4, 5, 6]
# For loopfor num in numbers: # conditional statements if num == 4: # If num equals 4, this "break" statement gets executed break print(num)
Output:
1
2
3
In this example, we used an if
statement to search for the number 4 within the list. As the loop iterates through the numbers, the break
statement is used to exit the loop immediately upon finding the number 4. Consequently, only the numbers 1, 2, and 3 are printed before the break
statement exits the loop. Early exit from a loop is an optimization strategy that saves computational resources and reduces the loop's execution time.
Using continue
to Skip an Iteration
Use continue
when you want to skip the rest of the current iteration and move to the next iteration of the loop.
Example
You are processing a list of numbers, and you only want to print odd numbers and skip even numebrs.
# Create a listnumbers = [1, 2, 3, 4, 5, 6]
# For loopfor num in numbers: # conditional statements if num % 2 == 0: # If num is even, the "continue" statement executes to skip the remaining code # and proceed to the next iteration continue print(num)
Output:
1
3
5
In this example, we utilize a for
loop to iterate through a list of numbers. We employ an if
statement with the condition num % 2 == 0
to identify even numbers. When num
is even, the continue
statement skips the remaining code, here it's the print(num)
. It allows the loop to advance to the next iteration. Conversely, when num
is odd and fails to meet the if
condition, print(num)
executes to print odd numbers.
Use pass
as a Placeholder
Use pass
as a placeholder if you need a loop structure with no immediate code inside, either for temporary holding or future implementation.
The pass
statement isn't limited to loops; it's also applicable in functions, classes, and other structures where a syntactical placeholder is required.
Example
Defining a loop structure intended for later development.
# Create a listnumbers = [1, 2, 3, 4, 5, 6]
# For loopfor item in numbers: # Placeholder for future code pass
This code iterates through the numbers
list, but it doesn't contain any meaningful code within the loop. To prevent Python from raising an error due to an empty loop body, the pass
statement is used as a placeholder, allowing the loop to remain syntactically valid.