0
0
PowerShellscripting~10 mins

Break and continue in PowerShell - Interactive Code Practice

Choose your learning style9 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.