0
0
PowerShellscripting~20 mins

While and Do-While loops in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery in PowerShell
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a While loop counting down
What is the output of this PowerShell script?
PowerShell
i=3
while ($i -gt 0) {
  Write-Output $i
  $i--
}
A
3
2
1
0
B
1
2
3
C
3
2
1
D
0
1
2
3
Attempts:
2 left
💡 Hint
Remember the loop runs while $i is greater than zero and decreases $i each time.
💻 Command Output
intermediate
2:00remaining
Output of a Do-While loop with a condition
What will this PowerShell script output?
PowerShell
i=1
do {
  Write-Output $i
  $i += 2
} while ($i -lt 6)
A
1
2
3
4
5
B
1
3
5
7
C
2
4
6
D
1
3
5
Attempts:
2 left
💡 Hint
The loop adds 2 to $i each time and continues while $i is less than 6.
🔧 Debug
advanced
2:00remaining
Identify the error in this While loop
What error will this PowerShell script produce?
PowerShell
i=0
while ($i < 3) {
  Write-Output $i
  i++
}
ASyntax error: Missing '$' before variable 'i' in increment
BRuntime error: Variable 'i' is not recognized
CInfinite loop printing 0 forever
DOutputs 0, 1, 2 correctly
Attempts:
2 left
💡 Hint
Check how variables are referenced and incremented in PowerShell.
🚀 Application
advanced
2:00remaining
Count how many times a Do-While loop runs
How many times will this PowerShell Do-While loop execute?
PowerShell
$count = 0
do {
  $count++
} while ($count -lt 5)
A5
B4
C6
DInfinite loop
Attempts:
2 left
💡 Hint
The loop runs while $count is less than 5 and increments $count each time.
🧠 Conceptual
expert
2:00remaining
Difference between While and Do-While loops in PowerShell
Which statement correctly describes the difference between While and Do-While loops in PowerShell?
AWhile loops run the body once before checking the condition; Do-While loops check the condition before running the body.
BWhile loops check the condition before running the loop body; Do-While loops run the body once before checking the condition.
CBoth loops check the condition after running the loop body.
DBoth loops check the condition before running the loop body.
Attempts:
2 left
💡 Hint
Think about when the condition is evaluated in each loop type.