Bird
Raised Fist0
PowerShellscripting~10 mins

Break and continue in PowerShell - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to stop the loop when the number is 5.

PowerShell
foreach ($i in 1..10) {
    if ($i -eq 5) {
        [1]
    }
    Write-Output $i
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Cexit
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' instead of 'break' will skip the current iteration but not stop the loop.
2fill in blank
medium

Complete the code to skip printing the number 3 but continue the loop.

PowerShell
foreach ($i in 1..5) {
    if ($i -eq 3) {
        [1]
    }
    Write-Output $i
}
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Cskip
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' will stop the loop entirely, not just skip one number.
3fill in blank
hard

Fix the error in the loop to skip even numbers and print only odd numbers.

PowerShell
foreach ($num in 1..6) {
    if ($num % 2 -eq 0) {
        [1]
    }
    Write-Output $num
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Cexit
Dskip
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' will stop the loop after the first even number.
4fill in blank
hard

Fill both blanks to stop the loop when number is 4 and skip printing number 2.

PowerShell
foreach ($i in 1..5) {
    if ($i -eq 2) {
        [1]
    }
    if ($i -eq 4) {
        [2]
    }
    Write-Output $i
}
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Cexit
Dskip
Attempts:
3 left
💡 Hint
Common Mistakes
Using break to skip number 2 will stop the loop too early.
5fill in blank
hard

Fill all three blanks to skip numbers less than 3, stop at 6, and print the rest.

PowerShell
foreach ($num in 1..7) {
    if ($num -lt [1]) {
        [2]
    }
    if ($num -eq [3]) {
        break
    }
    Write-Output $num
}
Drag options to blanks, or click blank then click option'
A3
Bcontinue
C6
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' instead of 'continue' will stop the entire script.

Practice

(1/5)
1. What does the break statement do inside a PowerShell loop?
easy
A. Stops the loop immediately and exits it
B. Skips the current iteration and continues with the next
C. Restarts the loop from the beginning
D. Pauses the loop until a condition is met

Solution

  1. Step 1: Understand the purpose of break

    The break statement is used to exit a loop immediately, no matter where it is in the loop.
  2. Step 2: Compare with other options

    continue skips to the next iteration, but break stops the loop entirely.
  3. Final Answer:

    Stops the loop immediately and exits it -> Option A
  4. Quick Check:

    break stops loop [OK]
Hint: Remember: break = stop loop, continue = skip iteration [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break skips iteration instead of stopping
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a PowerShell foreach loop?
easy
A. skip
B. continue
C. break
D. next

Solution

  1. Step 1: Identify the keyword to skip iteration

    In PowerShell, continue is the keyword used to skip the current iteration and move to the next one.
  2. Step 2: Verify other options

    break stops the loop, skip and next are not valid PowerShell keywords for this purpose.
  3. Final Answer:

    continue -> Option B
  4. Quick Check:

    continue skips iteration [OK]
Hint: Use continue to skip iteration, break to stop loop [OK]
Common Mistakes:
  • Using break instead of continue
  • Using non-existent keywords like skip or next
  • Confusing syntax with other languages
3. What will be the output of this PowerShell script?
foreach ($i in 1..5) {
  if ($i -eq 3) { continue }
  if ($i -eq 5) { break }
  Write-Output $i
}
medium
A. 1 2 4
B. 1 2 3 4
C. 1 2 4 5
D. 1 2

Solution

  1. Step 1: Analyze loop iterations and conditions

    Loop runs from 1 to 5. When $i is 3, continue skips output. When $i is 5, break stops loop before output.
  2. Step 2: Determine output values

    Outputs are for $i = 1, 2, and 4 only. 3 is skipped, 5 stops loop before output.
  3. Final Answer:

    1 2 4 -> Option A
  4. Quick Check:

    continue skips 3, break stops before 5 [OK]
Hint: Trace loop step-by-step, watch continue and break effects [OK]
Common Mistakes:
  • Including 3 in output despite continue
  • Including 5 in output despite break
  • Confusing continue and break effects
4. Identify the error in this PowerShell script snippet:
foreach ($num in 1..4) {
  if ($num -eq 2) {
    continue
  }
  elseif ($num -eq 3) {
    break
  }
  Write-Output $num
}
medium
A. The Write-Output is unreachable for $num = 3
B. The break statement should be outside the loop
C. No error; script runs as expected
D. The continue statement is used incorrectly

Solution

  1. Step 1: Review the use of continue and break

    continue correctly skips output when $num is 2. break stops loop when $num is 3.
  2. Step 2: Check output and flow

    Output will be 1 only. No syntax or logic errors present.
  3. Final Answer:

    No error; script runs as expected -> Option C
  4. 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

  1. Step 1: Understand conditions order

    We must stop loop when number > 7, so break must check that first to stop immediately.
  2. Step 2: Skip even numbers using continue

    After checking break, skip even numbers with continue to avoid output.
  3. Step 3: Verify output

    Outputs odd numbers from 1 to 7 only, then stops.
  4. Final Answer:

    Option D script -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard