Bird
0
0

You need to iterate numbers 1 to 10 in PowerShell, skipping odd numbers and stopping the loop once the number exceeds 6. Which script correctly uses break and continue?

hard📝 Application Q8 of 15
PowerShell - Control Flow
You need to iterate numbers 1 to 10 in PowerShell, skipping odd numbers and stopping the loop once the number exceeds 6. Which script correctly uses break and continue?
A<pre>foreach ($num in 1..10) { if ($num -gt 6) { continue } if ($num % 2 -eq 0) { break } Write-Output $num }</pre>
B<pre>foreach ($num in 1..10) { if ($num % 2 -ne 0) { continue } if ($num -gt 6) { break } Write-Output $num }</pre>
C<pre>foreach ($num in 1..10) { if ($num % 2 -eq 0) { continue } if ($num -gt 6) { break } Write-Output $num }</pre>
D<pre>foreach ($num in 1..10) { if ($num -gt 6) { break } if ($num % 2 -ne 0) { break } Write-Output $num }</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Skip odd numbers

    Use continue when number modulo 2 is not zero ($num % 2 -ne 0).
  2. Step 2: Stop when number exceeds 6

    Use break when $num -gt 6.
  3. Step 3: Output even numbers up to 6

    Write output only if above conditions are not met.
  4. Final Answer:

    foreach ($num in 1..10) {
      if ($num % 2 -ne 0) { continue }
      if ($num -gt 6) { break }
      Write-Output $num
    }
    correctly implements these conditions.
  5. Quick Check:

    Continue skips odd; break stops after 6 [OK]
Quick Trick: Continue skips unwanted; break stops loop [OK]
Common Mistakes:
  • Using break to skip iterations instead of continue
  • Incorrect condition order causing logic errors
  • Mixing conditions for break and continue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes