continue correctly skips output when $num is 2. break stops loop when $num is 3.
Step 2: Check output and flow
Output will be 1 only. No syntax or logic errors present.
Final Answer:
No error; script runs as expected -> Option C
Quick Check:
continue and break used correctly [OK]
Hint: Check if continue/break are inside loop and reachable [OK]
Common Mistakes:
Thinking break must be outside loop
Assuming continue causes error
Confusing unreachable code with error
5. You want to process numbers 1 to 10 but skip all even numbers and stop processing once you reach a number greater than 7. Which script correctly uses break and continue to achieve this?
hard
A.
foreach ($n in 1..10) {
if ($n % 2 -eq 0) { break }
if ($n -gt 7) { continue }
Write-Output $n
}
B.
foreach ($n in 1..10) {
if ($n % 2 -eq 0) { continue }
Write-Output $n
if ($n -gt 7) { break }
}
C.
foreach ($n in 1..10) {
if ($n -gt 7) { continue }
if ($n % 2 -eq 0) { break }
Write-Output $n
}
D.
foreach ($n in 1..10) {
if ($n -gt 7) { break }
if ($n % 2 -eq 0) { continue }
Write-Output $n
}
Solution
Step 1: Understand conditions order
We must stop loop when number > 7, so break must check that first to stop immediately.
Step 2: Skip even numbers using continue
After checking break, skip even numbers with continue to avoid output.
Step 3: Verify output
Outputs odd numbers from 1 to 7 only, then stops.
Final Answer:
Option D script -> Option D
Quick Check:
break before continue for correct flow [OK]
Hint: Check break condition before continue to stop early [OK]
Common Mistakes:
Placing continue before break causing wrong stop
Using break to skip even numbers instead of continue
Not stopping loop when number > 7
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.