Bird
0
0

How can you use a switch statement to run the same code for multiple matching values, like 'cat' and 'dog'?

hard📝 Application Q9 of 15
PowerShell - Control Flow
How can you use a switch statement to run the same code for multiple matching values, like 'cat' and 'dog'?
Aswitch ($animal) { 'cat' { 'Pet' } 'dog' { 'Pet' } default { 'Other' } }
Bswitch ($animal) { 'cat','dog' { 'Pet' } default { 'Other' } }
Cswitch ($animal) { 'cat' -or 'dog' { 'Pet' } default { 'Other' } }
Dswitch ($animal) { case 'cat','dog' { 'Pet' } default { 'Other' } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple matches in switch

    PowerShell allows comma-separated values in a single case to match multiple inputs.
  2. Step 2: Check options for correct syntax

    switch ($animal) { 'cat','dog' { 'Pet' } default { 'Other' } } correctly uses 'cat','dog' in one case. Others use invalid syntax or keywords.
  3. Final Answer:

    switch ($animal) { 'cat','dog' { 'Pet' } default { 'Other' } } -> Option B
  4. Quick Check:

    Comma separates multiple matches in one case [OK]
Quick Trick: Use commas to group multiple values in one case [OK]
Common Mistakes:
  • Using -or inside case
  • Adding 'case' keyword
  • Trying to use logical operators inside switch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes