3.1 Controlling Loops

Working with Loops

Break, Continue, and Pass Statements

In addition to the basic loop structures, Python provides control statements that allow you to modify the flow of loops. These include break, continue, and pass. These statements can be used to alter the normal flow of a loop, providing more control over how iterations are executed.

Break Statement

The break statement is used to exit a loop prematurely. When a break statement is encountered, the loop terminates immediately, and control is transferred to the statement following the loop.

Here’s an example:

for i in range(10):
    if i == 5:
        break
    print(i)
0
1
2
3
4

In this example, the loop will print numbers 0 to 4. When i becomes 5, the break statement is executed, and the loop is terminated.

Continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop. When Python encounters a continue statement, the remaining code in the loop body is skipped for the current iteration.

Here’s an example:

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)
1
3
5
7
9

This loop uses the modulus operator (%) to check if i is even. If it is, the continue statement is executed, skipping the print statement for that iteration. Due to the continue statement, we will only print the odd numbers from 0 to 9.

Important Caveats with continue:

  1. Code after continue is never executed: Any code written after a continue statement in the same iteration will be skipped entirely. This can lead to unexpected behavior if you’re not careful about placement.

  2. Performance considerations: While continue is useful for readability, excessive use can sometimes make code harder to follow. Consider whether restructuring your conditional logic might be clearer.

  3. In data processing: When processing datasets, continue is commonly used to skip invalid or missing data, but always ensure you’re not inadvertently skipping important error handling or cleanup code.

Pass Statement

The pass statement is a null operation. It does nothing when executed. It is often used as a placeholder in loops or conditionals where syntactically some code is required, but no action is needed. When building a loop, you might want to include a pass statement temporarily while you are still developing the code. This allows you to avoid syntax errors without implementing the logic yet. Other times, you might use pass in a loop to indicate that no action is needed for certain conditions, but you still want to maintain the structure of the loop. In this way, pass can operate similarly to continue, but it does not skip the iteration; it simply does nothing. I tend to use pass when I want to write a loop that I will fill in later, or when I want to create a loop that does nothing for certain conditions but still needs to be syntactically correct.

Here’s an example:

for i in range(10):
    if i % 2 == 0:
        pass
    else:
        print(i)
1
3
5
7
9

In this example, the pass statement is used for even numbers, and the loop will print only the odd numbers from 0 to 9.