What is the purpose of an if statement in PowerShell?
An if statement checks a condition and runs code only if that condition is true. It helps make decisions in scripts.
Click to reveal answer
beginner
How do you write an elseif statement in PowerShell?
Use the keyword 'elseif' followed by a condition in parentheses, then a script block with the code to run if that condition is true.
Click to reveal answer
beginner
What does the else block do in an if-elseif-else structure?
The else block runs code when none of the previous if or elseif conditions are true. It acts as a fallback.
Click to reveal answer
beginner
Show the basic syntax of an if-elseif-else statement in PowerShell.
if (<condition1>) {
# code if condition1 is true
} elseif (<condition2>) {
# code if condition2 is true
} else {
# code if none are true
}
Click to reveal answer
intermediate
Why is it useful to use elseif instead of multiple if statements?
Using elseif checks conditions one by one and stops when one is true, making scripts faster and easier to read compared to many separate ifs.
Click to reveal answer
What keyword starts an alternative condition after an if in PowerShell?
Aelif
Belse if
Celseif
Dotherwise
✗ Incorrect
PowerShell uses 'elseif' as one word to check another condition after an if.
What happens if none of the if or elseif conditions are true and there is an else block?
ANothing happens
BThe else block runs
CAn error occurs
DThe script stops
✗ Incorrect
The else block runs as a fallback when all previous conditions are false.
Which symbol is used to start a condition in PowerShell if statements?
A() parentheses
B[] brackets
C{} braces
D<> angle brackets
✗ Incorrect
Conditions in if statements are placed inside parentheses ().
Can you have multiple elseif blocks in one if statement?
ANo, elseif is not valid in PowerShell
BNo, only one elseif is allowed
CYes, but only two
DYes, you can have many elseif blocks
✗ Incorrect
PowerShell allows multiple elseif blocks to check several conditions in order.
What is the correct way to write an else block?
Aelse { # code }
Belse () { # code }
Celse if { # code }
Delseif { # code }
✗ Incorrect
The else block uses 'else' followed by a script block in braces {} without a condition.
Explain how if-elseif-else statements control the flow of a PowerShell script.
Think about how the script chooses which code to run based on conditions.
You got /5 concepts.
Write a simple PowerShell if-elseif-else script that checks a number and prints if it is positive, negative, or zero.
Use comparison operators like -gt and -lt for greater than and less than.
You got /4 concepts.
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
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."
Final Answer:
It chooses which code to run based on conditions. -> Option A
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
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.
Final Answer:
if ( $x -eq 5 ) { } -> Option B
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
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'.
Final Answer:
Equal to 7 -> Option A
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
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".
Final Answer:
Missing parentheses around first if condition -> Option D
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
Step 1: Analyze temperature ranges and verify conditions