0
0
PowerShellscripting~5 mins

While and Do-While loops in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a While loop in PowerShell?
A While loop repeats a block of code as long as a specified condition is true. It checks the condition before running the code each time.
Click to reveal answer
beginner
How does a Do-While loop differ from a While loop?
A Do-While loop runs the code block first, then checks the condition. This means it always runs at least once, even if the condition is false at the start.
Click to reveal answer
beginner
Write a simple PowerShell While loop that counts from 1 to 3.
$i = 1 while ($i -le 3) { Write-Output $i $i += 1 }
Click to reveal answer
beginner
What happens if the condition in a While loop is false at the start?
The code inside the While loop does not run at all because the condition is checked before the loop runs.
Click to reveal answer
beginner
Explain a real-life example of a Do-While loop.
Imagine you want to taste a soup and keep adding salt until it tastes right. You taste it first (run code), then decide if you need more salt (check condition). This is like a Do-While loop.
Click to reveal answer
In PowerShell, when does a While loop check its condition?
ABefore running the loop code
BAfter running the loop code
COnly once at the start
DIt never checks a condition
Which loop guarantees the code runs at least once?
AWhile loop
BDo-While loop
CFor loop
DNone of the above
What will happen if the While loop condition is false at the start?
AThe loop runs once
BThe loop runs infinitely
CThe loop runs twice
DThe loop does not run
Which symbol is used to increment a variable in PowerShell inside a loop?
A$i += 1
B++$i
CAll of the above
D$i++
What is the main purpose of loops like While and Do-While?
ATo write code faster
BTo stop code from running
CTo repeat code multiple times
DTo create variables
Describe how a While loop works in PowerShell and give a simple example.
Think about checking a rule before doing something repeatedly.
You got /3 concepts.
    Explain the difference between While and Do-While loops with a real-life analogy.
    Consider when you decide to repeat an action: before or after doing it once.
    You got /3 concepts.