Bird
Raised Fist0
PowerShellscripting~20 mins

If-elseif-else statements in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell If-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of nested if-elseif-else in PowerShell
What is the output of this PowerShell script?
PowerShell
param($num = 15)

if ($num -lt 10) {
    Write-Output "Less than 10"
} elseif ($num -lt 20) {
    Write-Output "Between 10 and 19"
} else {
    Write-Output "20 or more"
}
ALess than 10
BNo output
C20 or more
DBetween 10 and 19
Attempts:
2 left
💡 Hint
Check which condition matches the value 15.
💻 Command Output
intermediate
2:00remaining
PowerShell if-elseif-else with string comparison
What will this PowerShell script output when $color is set to 'blue'?
PowerShell
$color = 'blue'

if ($color -eq 'red') {
    Write-Output 'Stop'
} elseif ($color -eq 'yellow') {
    Write-Output 'Caution'
} elseif ($color -eq 'green') {
    Write-Output 'Go'
} else {
    Write-Output 'Unknown color'
}
AUnknown color
BCaution
CGo
DStop
Attempts:
2 left
💡 Hint
Check if 'blue' matches any of the specified colors.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this PowerShell if-elseif-else block
Which option contains the syntax error in this PowerShell code snippet?
PowerShell
if ($x -gt 10) {
    Write-Output 'Greater than 10'
} elseif ($x -lt 5) {
    Write-Output 'Less than 5'
} else
    Write-Output 'Between 5 and 10'
AMissing closing brace after elseif block
BMissing opening brace after else
CIncorrect comparison operator in if condition
DMissing dollar sign in variable name
Attempts:
2 left
💡 Hint
Check the else block syntax carefully.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell script not output anything?
Given this script, why is there no output?
PowerShell
$value = 0

if ($value) {
    Write-Output 'Value is true'
} elseif (-not $value) {
    Write-Output 'Value is false'
} else {
    Write-Output 'Value is unknown'
}
ABecause 0 is treated as false, so the if block is skipped and elseif condition is true, but Write-Output is not called
BBecause PowerShell treats 0 as true, so the if block runs but Write-Output is missing
CBecause 0 is treated as false, the if block is skipped, the elseif condition is true, so it outputs 'Value is false'
DBecause the script has a syntax error and does not run
Attempts:
2 left
💡 Hint
Remember how PowerShell treats 0 in boolean context.
🚀 Application
expert
3:00remaining
Determine the final value of $result after this PowerShell script runs
What is the value of $result after running this script?
PowerShell
$score = 85

if ($score -ge 90) {
    $result = 'A'
} elseif ($score -ge 80) {
    $result = 'B'
} elseif ($score -ge 70) {
    $result = 'C'
} elseif ($score -ge 60) {
    $result = 'D'
} else {
    $result = 'F'
}
A'B'
B'A'
C'C'
D'F'
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches 85 first.

Practice

(1/5)
1. What does an if-elseif-else statement do in PowerShell?
easy
A. It chooses which code to run based on conditions.
B. It repeats code multiple times.
C. It stores data in variables.
D. It defines a function.

Solution

  1. Step 1: Identify the purpose of if-elseif-else

    An if-elseif-else statement checks conditions and runs code based on which condition is true. This matches "It chooses which code to run based on conditions."
  2. Final Answer:

    It chooses which code to run based on conditions. -> Option A
  3. Quick Check:

    if-elseif-else controls flow by conditions = B [OK]
Hint: If it picks code based on true/false, it's if-elseif-else [OK]
Common Mistakes:
  • Confusing if-elseif-else with loops
  • Thinking it stores data
  • Mixing it up with function definitions
2. Which of the following is the correct syntax to start an if statement in PowerShell?
easy
A. if $x == 5 then { }
B. if ( $x -eq 5 ) { }
C. if $x = 5 { }
D. if $x equals 5 { }

Solution

  1. Step 1: Recall PowerShell if syntax and verify options

    PowerShell uses if (condition) { code } with -eq for equality check. "if ( $x -eq 5 ) { }" uses correct parentheses, -eq operator, and braces. Others use wrong operators or keywords.
  2. Final Answer:

    if ( $x -eq 5 ) { } -> Option B
  3. Quick Check:

    PowerShell if: parens + -eq = A [OK]
Hint: Use parentheses and -eq for conditions in if [OK]
Common Mistakes:
  • Using == instead of -eq
  • Omitting parentheses
  • Using = for comparison
3. What will this PowerShell code output?
 $num = 7
if ($num -lt 5) {
'Less than 5'
} elseif ($num -eq 7) {
'Equal to 7'
} else {
'Other'
}
medium
A. Equal to 7
B. No output
C. Other
D. Less than 5

Solution

  1. Step 1: Trace code execution step-by-step

    $num is 7, ($num -lt 5) is false. ($num -eq 7) is true, outputs 'Equal to 7'.
  2. Final Answer:

    Equal to 7 -> Option A
  3. Quick Check:

    7 -eq 7 triggers elseif = D [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing first if block without checking condition
  • Ignoring elseif block
  • Assuming else runs always
4. Find the error in this PowerShell snippet:
if $value -gt 10 {
'Greater than 10'
} elseif ($value -eq 10) {
'Equal to 10'
} else {
'Less than 10'
}
medium
A. Braces {} are not allowed
B. Using -gt instead of >
C. Missing else keyword
D. Missing parentheses around first if condition

Solution

  1. Step 1: Check syntax and identify the error

    PowerShell requires parentheses around the condition in if statements. The first if lacks them: "if $value -gt 10 {" should be "if ($value -gt 10) {". Matches "Missing parentheses around first if condition".
  2. Final Answer:

    Missing parentheses around first if condition -> Option D
  3. Quick Check:

    All if conditions need parentheses = A [OK]
Hint: Always put conditions inside parentheses in if [OK]
Common Mistakes:
  • Omitting parentheses in if
  • Confusing -gt with > (both valid but -gt preferred)
  • Forgetting else keyword
5. You want to write a script that prints:
- 'Cold' if temperature is below 10,
- 'Warm' if temperature is between 10 and 25 inclusive,
- 'Hot' if temperature is above 25.
Which if-elseif-else structure correctly implements this?
hard
A. if ($temp -lt 10) { 'Cold' } elseif ($temp -ge 25) { 'Hot' } else { 'Warm' }
B. if ($temp -le 10) { 'Cold' } elseif ($temp -lt 25) { 'Warm' } else { 'Hot' }
C. if ($temp -lt 10) { 'Cold' } elseif ($temp -le 25) { 'Warm' } else { 'Hot' }
D. if ($temp -gt 25) { 'Hot' } elseif ($temp -gt 10) { 'Warm' } else { 'Cold' }

Solution

  1. Step 1: Analyze temperature ranges and verify conditions

    Cold: below 10, Warm: 10-25 inclusive, Hot: above 25. "if ($temp -lt 10) { 'Cold' } elseif ($temp -le 25) { 'Warm' } else { 'Hot' }": <10=Cold, <=25=Warm (10-25), else Hot. Matches exactly.
  2. Final Answer:

    if ($temp -lt 10) { 'Cold' } elseif ($temp -le 25) { 'Warm' } else { 'Hot' } -> Option C
  3. Quick Check:

    Ranges covered correctly = C [OK]
Hint: Order conditions from smallest to largest range [OK]
Common Mistakes:
  • Mixing up less than and less or equal
  • Wrong order causing wrong output
  • Overlapping or missing ranges
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