Bird
0
0

You have this PowerShell code:

hard📝 Application Q9 of 15
PowerShell - Control Flow
You have this PowerShell code:
$val = 5
if ($val -eq 5) { 'Five' } elseif ($val -eq 10) { 'Ten' } else { 'Other' }

How would you modify it to also print 'Five or Ten' if $val is either 5 or 10, without repeating code?
AAdd another elseif with <code>$val -eq 5 -and $val -eq 10</code>
BUse <code>if ($val -eq 5 -or $val -eq 10) { 'Five or Ten' } else { 'Other' }</code>
CUse nested if inside else block
DReplace all conditions with <code>if ($val)</code>
Step-by-Step Solution
Solution:
  1. Step 1: Combine conditions with -or

    Use -or to check if $val equals 5 or 10 in one if.
  2. Step 2: Simplify code to avoid repetition

    This avoids repeating print statements for 5 and 10 separately.
  3. Final Answer:

    Use if ($val -eq 5 -or $val -eq 10) { 'Five or Ten' } else { 'Other' } -> Option B
  4. Quick Check:

    Use -or to combine conditions [OK]
Quick Trick: Use -or to combine multiple conditions [OK]
Common Mistakes:
  • Using -and instead of -or
  • Trying to check both values at once with -and
  • Adding unnecessary nested ifs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes