Recall & Review
beginner
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?
✗ 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?
✗ 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?
✗ Incorrect
Conditions in if statements are placed inside parentheses ().
Can you have multiple elseif blocks in one if statement?
✗ Incorrect
PowerShell allows multiple elseif blocks to check several conditions in order.
What is the correct way to write an else block?
✗ 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.