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.
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
✗ Incorrect
A While loop checks the condition before running the code block each time.
Which loop guarantees the code runs at least once?
AWhile loop
BDo-While loop
CFor loop
DNone of the above
✗ Incorrect
A Do-While loop runs the code first, then checks the condition, so it always runs at least once.
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
✗ Incorrect
If the condition is false at the start, the While loop does not run at all.
Which symbol is used to increment a variable in PowerShell inside a loop?
A$i += 1
B++$i
CAll of the above
D$i++
✗ Incorrect
$i += 1 is the standard and compatible way to increment a variable by 1 in PowerShell.
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
✗ Incorrect
Loops repeat a block of code multiple times based on a condition.
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.
Practice
(1/5)
1. What is the main difference between a while loop and a do-while loop in PowerShell?
easy
A. while checks the condition before running the loop body; do-while runs the loop body once before checking.
B. while runs the loop body once before checking; do-while checks the condition before running.
C. while loops only run once; do-while loops run multiple times.
D. while loops are faster than do-while loops.
Solution
Step 1: Understand while loop behavior
The while loop checks the condition before running the code inside the loop. If the condition is false initially, the loop body does not run at all.
Step 2: Understand do-while loop behavior
The do-while loop runs the code inside the loop once before checking the condition. It then repeats if the condition is true.
Final Answer:
while checks the condition before running the loop body; do-while runs the loop body once before checking. -> Option A
Quick Check:
Loop condition check timing = A [OK]
Hint: Remember: while checks first, do-while runs once first [OK]
Common Mistakes:
Thinking both loops check condition before running
Confusing which loop runs code first
Assuming do-while runs only once
Believing while loops always run at least once
2. Which of the following is the correct syntax for a do-while loop in PowerShell?
easy
A. do (commands) while { condition }
B. while (condition) { commands }
C. do { commands } while (condition)
D. while { condition } do { commands }
Solution
Step 1: Recall do-while syntax
In PowerShell, the do-while loop syntax is: do { commands } while (condition). The loop runs the commands first, then checks the condition.
Step 2: Check each option
do { commands } while (condition) matches the correct syntax exactly. Options C and D have incorrect placement of braces and parentheses. while (condition) { commands } is the syntax for a while loop, not do-while.
Final Answer:
do { commands } while (condition) -> Option C
Quick Check:
Correct do-while syntax = A [OK]
Hint: Remember: do { } while (condition) is the correct form [OK]
Common Mistakes:
Mixing while and do-while syntax
Using parentheses and braces incorrectly
Placing condition before the loop body
Confusing syntax with other languages
3. What will be the output of this PowerShell code?
$count = 1 do { Write-Output $count $count++ } while ($count -le 3)
medium
A. No output
B. 1 2
C. 2 3
D. 1 2 3
Solution
Step 1: Understand the loop execution
The do-while loop runs the body first, printing $count starting at 1, then increments $count.
Step 2: Trace each iteration
Iteration 1: prints 1, $count becomes 2 Iteration 2: prints 2, $count becomes 3 Iteration 3: prints 3, $count becomes 4 Condition $count -le 3 fails, loop stops.
Final Answer:
1 2 3 -> Option D
Quick Check:
Loop prints 1 to 3 [OK]
Hint: Remember do-while runs once before condition check [OK]
Common Mistakes:
Thinking loop stops before printing 3
Confusing increment timing
Assuming no output if condition false initially
Mixing output order
4. Identify the error in this PowerShell while loop code:
$i = 5 while ($i -lt 5) { Write-Output $i $i++ }
medium
A. The loop condition is never true, so loop body never runs.
B. The increment operator $i++ is invalid in PowerShell.
C. Missing parentheses around the loop body.
D. The variable $i is not initialized.
Solution
Step 1: Check initial condition
The variable $i is initialized to 5. The loop condition is $i -lt 5 (less than 5). Since 5 is not less than 5, the condition is false initially.
Step 2: Understand loop execution
Because the condition is false at the start, the while loop body never runs, so no output is produced.
Final Answer:
The loop condition is never true, so loop body never runs. -> Option A
Quick Check:
Initial condition false means no loop execution [OK]
Hint: Check if loop condition is true initially to run loop [OK]
Common Mistakes:
Assuming loop runs at least once
Thinking $i++ is invalid in PowerShell
Confusing syntax for loop body braces
Ignoring initial variable value
5. You want to write a PowerShell script that asks the user to enter a number between 1 and 5. It should keep asking until the user enters a valid number. Which loop type is best and why?
hard
A. Use a for loop because it repeats a fixed number of times.
B. Use a do-while loop because it runs the prompt at least once before checking the input.
C. Use a while loop because it checks the input before running the prompt.
D. Use an if statement because it handles repeated input.
Solution
Step 1: Understand the input requirement
The script must ask the user at least once to enter a number, then repeat if invalid.
Step 2: Choose loop type based on behavior
A do-while loop runs the prompt first, then checks the condition. This fits the need to ask at least once and repeat if needed. A while loop checks condition first, which is not suitable here because input is not available before first prompt.
Final Answer:
Use a do-while loop because it runs the prompt at least once before checking the input. -> Option B
Quick Check:
Prompt at least once = do-while [OK]
Hint: Use do-while to run code once before condition check [OK]
Common Mistakes:
Choosing while loop and expecting prompt to run first
Using for loop for unknown repeat count
Using if statement for repeated input
Not considering first prompt must always run
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.