Bird
Raised Fist0
PowerShellscripting~20 mins

Why control flow directs execution in PowerShell - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Control Flow Mastery
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 script with if-else?
Look at this script and tell what it prints when $value is 5.
PowerShell
$value = 5
if ($value -gt 10) {
  Write-Output "Greater than 10"
} else {
  Write-Output "10 or less"
}
A10 or less
BGreater than 10
CNo output
DError: Variable not defined
Attempts:
2 left
💡 Hint
Think about whether 5 is greater than 10 or not.
🧠 Conceptual
intermediate
2:00remaining
Why does the script skip the else block here?
Given this script, why does it print "Inside if" and skip the else block?
PowerShell
$x = 20
if ($x -eq 20) {
  Write-Output "Inside if"
} else {
  Write-Output "Inside else"
}
ABecause $x is not defined
BBecause else always runs after if
CBecause $x equals 20, so the if condition is true
DBecause Write-Output only works inside if
Attempts:
2 left
💡 Hint
Check the condition inside the if statement.
📝 Syntax
advanced
2:00remaining
Which option correctly uses a switch statement to print 'Fruit' for apple and banana?
Choose the correct PowerShell switch syntax that prints 'Fruit' when the input is 'apple' or 'banana'.
PowerShell
switch ($item) {
  'apple' { Write-Output 'Fruit' }
  'banana' { Write-Output 'Fruit' }
  default { Write-Output 'Unknown' }
}
A
switch ($item) {
  'apple': Write-Output 'Fruit'
  'banana': Write-Output 'Fruit'
  default: Write-Output 'Unknown'
}
B
switch ($item) {
  'apple' { Write-Output 'Fruit' }
  'banana' { Write-Output 'Fruit' }
  default { Write-Output 'Unknown' }
}
C
switch ($item) {
  'apple' -> Write-Output 'Fruit'
  'banana' -> Write-Output 'Fruit'
  default -> Write-Output 'Unknown'
}
D
switch $item {
  'apple' => Write-Output 'Fruit'
  'banana' => Write-Output 'Fruit'
  default => Write-Output 'Unknown'
}
Attempts:
2 left
💡 Hint
PowerShell switch uses braces {} for code blocks, not arrows or colons.
🔧 Debug
advanced
2:00remaining
What error does this script produce and why?
This script tries to use a while loop but has a mistake. What error will it produce?
PowerShell
$count = 3
while $count -gt 0 {
  Write-Output $count
  $count--
}
ASyntaxError: Missing parentheses around while condition
BRuntime error: Variable $count not found
CSyntaxError: Missing parentheses around condition
DNo error, prints 3 2 1
Attempts:
2 left
💡 Hint
Check how the while condition is written in PowerShell.
🚀 Application
expert
2:00remaining
How many times does this nested loop print 'Looping'?
Count how many times 'Looping' is printed by this script.
PowerShell
$outer = 2
$inner = 3
for ($i = 0; $i -lt $outer; $i++) {
  for ($j = 0; $j -lt $inner; $j++) {
    Write-Output 'Looping'
  }
}
A5 times
B2 times
C3 times
D6 times
Attempts:
2 left
💡 Hint
Multiply the number of outer loop runs by inner loop runs.

Practice

(1/5)
1. What does control flow in a PowerShell script primarily do?
easy
A. It changes the script's color scheme.
B. It decides which parts of the script run and when.
C. It increases the script's speed automatically.
D. It saves the script to a file.

Solution

  1. Step 1: Understand control flow purpose

    Control flow directs the order and conditions under which code runs.
  2. Step 2: Match purpose to options

    Only It decides which parts of the script run and when. describes directing execution timing and choice.
  3. Final Answer:

    It decides which parts of the script run and when. -> Option B
  4. Quick Check:

    Control flow = directing execution [OK]
Hint: Control flow controls script steps, not appearance or speed [OK]
Common Mistakes:
  • Confusing control flow with script styling
  • Thinking control flow saves files
  • Assuming control flow speeds up code automatically
2. Which of the following is the correct syntax to start an if statement in PowerShell?
easy
A. if (condition) {
B. if condition then {
C. if condition:
D. if condition {}

Solution

  1. Step 1: Recall PowerShell if syntax

    PowerShell uses if (condition) { ... } with parentheses and braces.
  2. Step 2: Compare options

    Only if (condition) { matches this syntax exactly.
  3. Final Answer:

    if (condition) { -> Option A
  4. Quick Check:

    PowerShell if uses parentheses and braces [OK]
Hint: PowerShell if always uses parentheses and braces [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using 'then' keyword (not used in PowerShell)
  • Using colon instead of braces
3. What will this PowerShell code output?
for ($i = 1; $i -le 3; $i++) { if ($i -eq 2) { continue } Write-Output $i }
medium
A. 1
B. 1 2 3
C. 2 3
D. 1 3

Solution

  1. Step 1: Understand the loop and continue

    The loop runs $i from 1 to 3. When $i equals 2, 'continue' skips the rest of that iteration.
  2. Step 2: Trace output values

    Output occurs only when $i is not 2, so outputs are 1 and 3.
  3. Final Answer:

    1 3 -> Option D
  4. Quick Check:

    continue skips 2, outputs 1 and 3 [OK]
Hint: continue skips current loop step, so that value is not output [OK]
Common Mistakes:
  • Thinking continue outputs the skipped value
  • Confusing continue with break
  • Ignoring the if condition
4. Identify the error in this PowerShell snippet:
if $x -gt 10 { Write-Output 'Big' }
medium
A. Missing curly braces around the block.
B. Incorrect comparison operator.
C. Missing parentheses around the condition.
D. Write-Output cannot be used inside if.

Solution

  1. Step 1: Check if statement syntax

    PowerShell requires parentheses around the condition in if statements.
  2. Step 2: Verify given code

    The code lacks parentheses around '$x -gt 10', causing a syntax error.
  3. Final Answer:

    Missing parentheses around the condition. -> Option C
  4. Quick Check:

    if condition must be in parentheses [OK]
Hint: Always put conditions inside parentheses in if statements [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Thinking curly braces are missing (they are present)
  • Believing Write-Output is invalid inside if
5. You want to run a script block repeatedly until a variable $count reaches 5, but skip printing when $count is 3. Which control flow structure correctly achieves this?
$count = 0
while ($count -lt 5) {
$count++
if ($count -eq 3) { continue }
Write-Output $count
}
hard
A. The script uses while loop with continue to skip printing 3.
B. The script should use if-else instead of continue to skip 3.
C. The script must use a for loop, not while, to skip 3.
D. The script will print 3 because continue does not skip output.

Solution

  1. Step 1: Analyze loop and continue usage

    The while loop increments $count until 5. When $count is 3, continue skips Write-Output.
  2. Step 2: Confirm output behavior

    All numbers except 3 are printed, matching the requirement.
  3. Final Answer:

    The script uses while loop with continue to skip printing 3. -> Option A
  4. Quick Check:

    continue skips printing 3 in while loop [OK]
Hint: Use continue inside loops to skip specific iterations [OK]
Common Mistakes:
  • Thinking continue prints skipped values
  • Believing for loop is required here
  • Replacing continue with if-else unnecessarily
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