📖 Scenario: You are managing a list of daily tasks. You want to process these tasks one by one but stop processing when you find a specific task. Also, you want to skip certain tasks that are not important.
🎯 Goal: Build a PowerShell script that loops through a list of tasks, skips unimportant tasks, and stops processing when a specific task is found.
📋 What You'll Learn
Create a list of tasks with exact names
Create a variable to hold the task to stop at
Use a foreach loop to go through the tasks
Use continue to skip unimportant tasks
Use break to stop when the stop task is found
Print the tasks that are processed
💡 Why This Matters
🌍 Real World
Automating task processing where some tasks need to be skipped and processing stops at a certain point.
💼 Career
Understanding break and continue is essential for controlling loops in scripts used for automation, data processing, and system administration.
Progress0 / 4 steps
1
Create the list of tasks
Create a list called $tasks with these exact tasks: 'Email', 'Meeting', 'Coffee', 'Report', 'Lunch'.
PowerShell
Hint
Use @() to create an array in PowerShell.
2
Set the stop task
Create a variable called $stopTask and set it to the string 'Report'.
PowerShell
Hint
Use = to assign the string 'Report' to $stopTask.
3
Loop with break and continue
Write a foreach loop using $task to go through $tasks. Inside the loop, use continue to skip the task 'Coffee'. Use break to stop the loop when $task equals $stopTask. For other tasks, print $task.
PowerShell
Hint
Use foreach ($task in $tasks) to loop. Use if statements to check tasks. Use Write-Output to print.
4
Print the processed tasks
Run the script to print the tasks processed before stopping. The output should show Email and Meeting only.
PowerShell
Hint
Run the script and check the output shows only Email and Meeting.
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
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.
Step 2: Compare with other options
continue skips to the next iteration, but break stops the loop entirely.
Final Answer:
Stops the loop immediately and exits it -> Option A
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.