Bird
0
0

Which of the following is the correct syntax to write an if-elseif-else statement in PowerShell?

easy📝 Syntax Q3 of 15
PowerShell - Control Flow
Which of the following is the correct syntax to write an if-elseif-else statement in PowerShell?
Aif ($x = 10) { 'Ten' } elseif ($x = 20) { 'Twenty' } else { 'Other' }
Bif $x == 10 then 'Ten' elseif $x == 20 then 'Twenty' else 'Other'
Cif ($x -eq 10) { 'Ten' } elseif ($x -eq 20) { 'Twenty' } else { 'Other' }
Dif $x equals 10 { 'Ten' } elseif $x equals 20 { 'Twenty' } else { 'Other' }
Step-by-Step Solution
Solution:
  1. Step 1: Check PowerShell condition syntax

    PowerShell uses -eq for equality and parentheses around conditions.
  2. Step 2: Verify block syntax

    Curly braces {} enclose code blocks for if, elseif, and else.
  3. Final Answer:

    if ($x -eq 10) { 'Ten' } elseif ($x -eq 20) { 'Twenty' } else { 'Other' } -> Option C
  4. Quick Check:

    Correct syntax uses -eq and braces [OK]
Quick Trick: Use -eq for equality and braces for blocks [OK]
Common Mistakes:
  • Using = instead of -eq for comparison
  • Omitting parentheses around conditions
  • Using 'then' keyword which PowerShell doesn't use

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes