The % operator is the modulus operator in programming, which returns the remainder of a division operation. When used in conjunction with the continue statement, it can control the flow of a loop based on certain conditions.
Here's an example in Python to illustrate how the % operator works with the continue statement:
for i in range(10):
if i % 2 == 0: # Check if the number is even
continue # Skip the rest of the loop for even numbers
print(i) # This will only print odd numbers
In this example:
- The loop iterates over numbers from 0 to 9.
- The condition
i % 2 == 0checks ifiis even. - If
iis even, thecontinuestatement is executed, which skips the current iteration and moves to the next one. - As a result, only odd numbers (1, 3, 5, 7, 9) are printed.
This demonstrates how the % operator can be used to determine whether to continue to the next iteration of a loop.
