0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.