Bird
Raised Fist0
PowerShellscripting~20 mins

For loop in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell for loop?
Consider the following PowerShell script:
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 }
A
3
2
1
B
0
1
2
3
C1 2 3
D
1
2
3
Attempts:
2 left
💡 Hint
Remember the loop starts at 1 and runs while $i is less than or equal to 3.
📝 Syntax
intermediate
2: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.
Afor ($i=0; $i -lt 5; $i++) { Write-Output $i }
Bfor $i = 0; $i -lt 5; $i++ { Write-Output $i }
Cfor ($i = 0; $i -lt 5; $i++) { Write-Output $i }
Dfor ($i=0; $i -le 4; $i++) { Write-Output $i }
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header carefully.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell for loop not output anything?
Look at this script:
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 }
ABecause the loop condition is false at the start, so the loop body never runs.
BBecause the loop variable $i is not initialized.
CBecause Write-Output is not the correct command to print in PowerShell.
DBecause $i is incremented incorrectly causing an infinite loop.
Attempts:
2 left
💡 Hint
Check the initial value and the loop condition carefully.
🚀 Application
advanced
2:00remaining
How many times will this PowerShell for loop run?
Given this script:
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 }
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Count from 10 down to 5 inclusive.
🧠 Conceptual
expert
2:00remaining
What is the value of $sum after this PowerShell for loop?
Consider this script:
$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 }
A4
B9
C10
D0
Attempts:
2 left
💡 Hint
Add the numbers 1 through 4 together.

Practice

(1/5)
1.

What does a for loop in PowerShell do?

easy
A. Defines a function
B. Runs code only once
C. Stops the script immediately
D. Repeats a block of code a set number of times

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is designed to repeat code multiple times based on a condition.
  2. Step 2: Compare options with the definition

    Only Repeats a block of code a set number of times correctly describes repeating code a set number of times.
  3. Final Answer:

    Repeats a block of code a set number of times -> Option D
  4. Quick Check:

    For loop = repeat code [OK]
Hint: For loops repeat code multiple times [OK]
Common Mistakes:
  • Thinking for loop runs only once
  • Confusing for loop with function definition
  • Believing for loop stops script
2.

Which of the following is the correct syntax for a for loop in PowerShell?

for (init; condition; increment) { code }
easy
A. for ($i=0; $i -lt 5; $i++) { Write-Output $i }
B. for $i in 0..5 { Write-Output $i }
C. foreach ($i=0; $i -lt 5; $i++) { Write-Output $i }
D. while ($i=0; $i -lt 5; $i++) { Write-Output $i }

Solution

  1. Step 1: Identify correct for loop syntax

    PowerShell for loops use parentheses with init; condition; increment separated by semicolons.
  2. Step 2: Check each option

    for ($i=0; $i -lt 5; $i++) { Write-Output $i } matches correct syntax. A uses while keyword. B has invalid 'for $i in' syntax. C uses foreach keyword. [OK]
  3. Final Answer:

    for ($i=0; $i -lt 5; $i++) { Write-Output $i } -> Option A
  4. Quick Check:

    Correct for loop syntax = for ($i=0; $i -lt 5; $i++) { Write-Output $i } [OK]
Hint: For loop uses semicolons inside parentheses [OK]
Common Mistakes:
  • Using foreach instead of for
  • Using spaces instead of semicolons
  • Confusing while loop syntax
3.

What will be the output of this PowerShell code?

for ($i=1; $i -le 3; $i++) { Write-Output ($i * 2) }
medium
A. 0 2 4
B. 1 2 3
C. 2 4 6
D. 3 6 9

Solution

  1. Step 1: Understand loop iterations

    The loop runs with $i values 1, 2, and 3 because condition is $i -le 3.
  2. Step 2: Calculate output for each iteration

    Output is $i * 2: 1*2=2, 2*2=4, 3*2=6.
  3. Final Answer:

    2 4 6 -> Option C
  4. Quick Check:

    Multiply each i by 2 = 2 4 6 [OK]
Hint: Multiply loop variable by 2 each iteration [OK]
Common Mistakes:
  • Confusing loop range
  • Multiplying after loop ends
  • Printing loop variable instead of calculation
4.

Identify the error in this PowerShell for loop:

for (; $i < 5; $i++) { Write-Output $i }
medium
A. The increment operator '++' is invalid in PowerShell
B. The loop variable $i is not initialized
C. The condition uses '<' instead of '-lt'
D. The loop body is missing curly braces

Solution

  1. Step 1: Check initialization syntax in for loop

    The initialization part is empty; $i needs to be initialized (e.g., $i=0).
  2. Step 2: Verify other parts

    Increment '++' is valid, condition '<' works for numbers, braces are present.
  3. Final Answer:

    The loop variable $i is not initialized -> Option B
  4. Quick Check:

    Always initialize $i in for loop [OK]
Hint: Initialize loop variable in for loop [OK]
Common Mistakes:
  • Forgetting to initialize loop variable
  • Thinking '++' is invalid
  • Using C-style operators like '<' in PowerShell
5.

You want to print only even numbers from 2 to 10 using a for loop in PowerShell. Which code snippet achieves this?

hard
A. for ($i=2; $i -le 10; $i+=2) { Write-Output $i }
B. for ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } }
C. for ($i=2; $i -lt 10; $i++) { Write-Output $i }
D. for ($i=0; $i -le 10; $i+=3) { Write-Output $i }

Solution

  1. Step 1: Understand the goal

    We want to print even numbers from 2 to 10 inclusive.
  2. Step 2: Analyze each option

    for ($i=1; $i -lt 10; $i++) { if ($i % 2 -eq 0) { Write-Output $i } } goes to i<10 so prints 2,4,6,8 (misses 10). for ($i=2; $i -le 10; $i+=2) { Write-Output $i } starts at 2, increments by 2 up to 10: 2,4,6,8,10. for ($i=2; $i -lt 10; $i++) { Write-Output $i } prints 2-9 all numbers. for ($i=0; $i -le 10; $i+=3) { Write-Output $i } wrong start/step.
  3. Final Answer:

    for ($i=2; $i -le 10; $i+=2) { Write-Output $i } -> Option A
  4. Quick Check:

    Increment by 2 from 2 to 10 = even numbers [OK]
Hint: Increment by 2 starting at 2 for even numbers [OK]
Common Mistakes:
  • Using wrong increment step
  • Starting from 1 instead of 2
  • Not filtering for even numbers
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