Challenge 2 — Solution
Task: Use a while loop to echo every even number from 2 to 20. Then
rewrite the same logic using continue inside a for loop that checks
every number from 1 to 20, skipping the odd ones.
";
$n += 2;
}
echo "---
";
// for + continue version
for ($i = 1; $i <= 20; $i++) {
if ($i % 2 !== 0) {
continue; // skip odd numbers entirely
}
echo "$i
";
}
?>
Output:
2
4
6
...
20
---
2
4
6
...
20
Notes:
- The while version only ever touches even numbers directly, stepping
by 2 each time ($n += 2), so there's nothing to skip.
- The for + continue version checks every number from 1 to 20, but
continue immediately jumps to the next iteration whenever $i is odd
(i % 2 !== 0), so only the even numbers ever reach the echo line.
- Both approaches produce the identical output, but they get there
differently - one avoids generating odd numbers at all, the other
generates every number and explicitly skips the ones it doesn't want.