Bird
Raised Fist0
PowerShellscripting~10 mins

If-elseif-else statements in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If-elseif-else statements
Start
↓
Evaluate if condition
Yes|No↓
Execute if block
↓
End
↓
Execute elseif block
↓
End
No↓
Execute else block
↓
End
The script checks the if condition first. If true, it runs the if block and ends. If false, it checks elseif conditions in order. If none match, it runs the else block.
Execution Sample
PowerShell
if ($x -gt 10) {
  "Greater than 10"
} elseif ($x -eq 10) {
  "Equal to 10"
} else {
  "Less than 10"
}
This script checks if $x is greater than 10, equal to 10, or less than 10, and prints a message accordingly.
Execution Table
StepCondition CheckedCondition ResultBlock ExecutedOutput
1$x -gt 10FalseNo
2$x -eq 10TrueYesEqual to 10
3ElseN/ANo
💡 Condition '$x -eq 10' is True, so elseif block runs and execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$x10101010
Key Moments - 2 Insights
Why does the elseif block run instead of the else block?
Because the elseif condition ($x -eq 10) is True at step 2, so the script runs that block and skips the else block (see execution_table row 2 and 3).
What happens if the first if condition is True?
If the first if condition is True, the script runs the if block and skips all elseif and else blocks (not shown here but implied by the flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 2?
AGreater than 10
BLess than 10
CEqual to 10
DNo output
💡 Hint
Check the 'Output' column in execution_table row 2.
At which step does the script decide to run the elseif block?
AStep 2
BStep 3
CStep 1
DNever
💡 Hint
Look at the 'Block Executed' column in execution_table to see when 'Yes' appears for elseif.
If $x was 15, which block would run according to the flow?
Aelseif block
Bif block
Celse block
DNo block
💡 Hint
Refer to concept_flow and imagine $x -gt 10 would be True.
Concept Snapshot
If-elseif-else statements check conditions in order.
Syntax:
if (condition) { ... } elseif (condition) { ... } else { ... }
Only the first True condition block runs.
If none True, else block runs.
Used to choose between multiple options.
Full Transcript
This visual execution shows how PowerShell if-elseif-else statements work. The script checks the if condition first. If it is true, it runs the if block and stops. If false, it checks the elseif condition. If that is true, it runs the elseif block and stops. If none are true, it runs the else block. The example uses $x = 10. The first condition ($x -gt 10) is false, so it checks the elseif ($x -eq 10), which is true. It runs the elseif block and outputs "Equal to 10". The else block is skipped. Variables stay the same throughout. This helps decide between multiple choices in scripts.

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