script_19.py - Continue Statement Example
Code
Python
#!/usr/bin/env python3
"""Continue statement example - skip odd numbers"""
for i in range(1, 21):
if i % 2 != 0:
continue
print(i)
Explanation
Line 4: for i in range(1, 21):
- Iterates from 1 to 20
- Loop runs 20 times total
Line 5: if i % 2 != 0:
- != is "not equal to" operator
- i % 2 != 0 checks if number is odd
- Odd numbers have remainder 1 when divided by 2
Line 6: continue
- Skips rest of current iteration
- Jumps back to start of loop (next iteration)
- Code below continue doesn't execute for this iteration
- Loop doesn't exit (unlike break)
- Only skips this specific iteration
Line 7: print(i)
- Only executes for even numbers
- Odd numbers skip this due to continue
- Example: i=1 -> continue (skip print), i=2 -> print, i=3 -> continue, i=4 -> print
- Result: only even numbers printed