Challenge - 5 Problems
PowerShell For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell for loop?
Consider the following PowerShell script:
What will be the output when this script runs?
for ($i = 1; $i -le 3; $i++) { Write-Output $i }What will be the output when this script runs?
PowerShell
for ($i = 1; $i -le 3; $i++) { Write-Output $i }
Attempts:
2 left
💡 Hint
Remember the loop starts at 1 and runs while $i is less than or equal to 3.
✗ Incorrect
The loop starts at 1 and increments by 1 until it reaches 3, outputting each number on its own line.
📝 Syntax
intermediate2:00remaining
Which option contains a syntax error in the PowerShell for loop?
Identify the option that will cause a syntax error when running the PowerShell for loop.
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header carefully.
✗ Incorrect
Option B is missing parentheses around the loop control statement, which is required in PowerShell.
🔧 Debug
advanced2:00remaining
Why does this PowerShell for loop not output anything?
Look at this script:
Why does it produce no output?
for ($i = 5; $i -lt 3; $i++) { Write-Output $i }Why does it produce no output?
PowerShell
for ($i = 5; $i -lt 3; $i++) { Write-Output $i }
Attempts:
2 left
💡 Hint
Check the initial value and the loop condition carefully.
✗ Incorrect
The loop starts with $i = 5, but the condition $i -lt 3 (less than 3) is false, so the loop never runs.
🚀 Application
advanced2:00remaining
How many times will this PowerShell for loop run?
Given this script:
How many times does the loop execute?
for ($i = 10; $i -ge 5; $i--) { Write-Output $i }How many times does the loop execute?
PowerShell
for ($i = 10; $i -ge 5; $i--) { Write-Output $i }
Attempts:
2 left
💡 Hint
Count from 10 down to 5 inclusive.
✗ Incorrect
The loop runs for $i = 10, 9, 8, 7, 6, 5 which is 6 times.
🧠 Conceptual
expert2:00remaining
What is the value of $sum after this PowerShell for loop?
Consider this script:
What is the value of $sum after the loop finishes?
$sum = 0
for ($i = 1; $i -le 4; $i++) { $sum += $i }What is the value of $sum after the loop finishes?
PowerShell
$sum = 0 for ($i = 1; $i -le 4; $i++) { $sum += $i }
Attempts:
2 left
💡 Hint
Add the numbers 1 through 4 together.
✗ Incorrect
The loop adds 1 + 2 + 3 + 4 = 10 to $sum.