The continue Statement
Concept
The continue statement is used to skip the rest of the code inside a loop for the current iteration and immediately jump to the next iteration of the loop. Unlike the break statement, which terminates the entire loop, continue allows the loop to keep running but skips the remaining code for the current iteration.
Syntax
for item in iterable: if condition: continue # Other code
while condition: if condition: continue # Other code
- continue: When encountered, it skips the remaining code inside the loop for the current iteration and proceeds to the next iteration.
- condition: This is the condition that triggers the continue statement.
Behavior
- Skip to Next Iteration: When continue is executed, the rest of the code inside the loop for the current iteration is ignored, and the loop moves on to the next iteration (or checks the condition in the case of while loops).
- No Loop Termination: Unlike break, continue does not terminate the loop; it simply skips the rest of the code for the current iteration.
Examples
Skipping Elements in a for Loop
Example:
for number in range(10): if number % 2 == 0: continue print(number)
Explanation:
- The for loop iterates over numbers from 0 to 9.
- The condition number % 2 == 0 checks if the number is even.
- If the condition is true, continue is executed, which skips the print(number) statement for that iteration.
- Even numbers are therefore skipped, and only odd numbers (1, 3, 5, 7, 9) are printed.
Skipping Iterations in a while Loop
Example:
i = 0 while i < 10: i += 1 if i % 2 == 0: continue print(i)
Explanation:
- The while loop continues as long as i is less than 10.
- i is incremented in each iteration.
- If i is even, continue is executed, which skips the print(i) statement for that iteration.
- Even numbers are ignored, and only odd numbers (1, 3, 5, 7, 9) are printed.
Filtering Out Unwanted Values
Example:
values = [10, 15, -2, 8, -7, 0, 5] for value in values: if value < 0: continue print(value)
Explanation:
- The for loop iterates over the list values.
- If value is negative (value < 0), continue is executed, which skips the print(value) statement for that iteration.
- Negative values are therefore ignored, and only non-negative values (10, 15, 8, 0, 5) are printed.
Points to Note
- Usage with if: continue is often used with if statements to filter out or skip specific items while continuing the execution of the loop.
- No Loop Termination: Unlike break, continue does not terminate the loop. It only skips the remaining code for the current iteration.
- Code Clarity: Using continue can make code clearer by avoiding excessive nesting and improving the readability of loops.
Combining continue with Other Statements
You can combine continue with other statements for more complex logic:
Example:
for i in range(10): if i % 3 == 0: print(f"Number {i} is divisible by 3, skipping it.") continue elif i % 2 == 0: print(f"Number {i} is even but not divisible by 3.") continue print(f"Number {i} is odd and not divisible by 3.")
Explanation:
- The for loop processes numbers from 0 to 9.
- If a number is divisible by 3, a message is printed, and continue is used to skip further processing for that number.
- Numbers that are even but not divisible by 3 are also processed differently and skipped using continue.
- Remaining numbers, which are odd and not divisible by 3, are printed.
Post Views: 231