0
0
PowerShellscripting~5 mins

For loop in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a for loop in PowerShell?
A for loop is a control structure that repeats a block of code a specific number of times. It uses a counter to track iterations.
Click to reveal answer
beginner
What are the three parts of a PowerShell for loop header?
1. Initialization (sets the counter)<br>2. Condition (checks if the loop should continue)<br>3. Increment/Decrement (updates the counter each time)
Click to reveal answer
beginner
How do you write a for loop in PowerShell that counts from 1 to 5?
for ($i = 1; $i -le 5; $i++) {<br> Write-Output $i<br>}
Click to reveal answer
beginner
What happens if the condition in a for loop is never true?
The loop body does not run at all because the condition is checked before each iteration.
Click to reveal answer
intermediate
How can you exit a for loop early in PowerShell?
Use the break statement inside the loop to stop it immediately.
Click to reveal answer
Which part of the for loop controls how many times the loop runs?
AThe loop body
BThe initialization
CThe increment
DThe condition
What does this for loop do? for ($i=0; $i -lt 3; $i++) { Write-Output $i }
APrints nothing
BPrints 1, 2, 3
CPrints 0, 1, 2
DPrints 0, 1, 2, 3
What keyword stops a for loop immediately in PowerShell?
Abreak
Bstop
Cexit
Dcontinue
If the initialization is missing in a for loop, what happens?
AIt depends on the condition and increment
BThe loop will not run
CThe loop runs infinitely
DPowerShell throws an error
Which symbol is used for incrementing the counter by 1 in PowerShell for loops?
A$i--
B$i++
C$i+=2
D$i*=
Explain how a for loop works in PowerShell and write a simple example that prints numbers from 1 to 3.
Think about setting a counter, checking it, and increasing it each time.
You got /5 concepts.
    Describe how you can stop a for loop before it finishes all iterations in PowerShell.
    What keyword lets you jump out of the loop?
    You got /3 concepts.