Bird
0
0

How can you simplify this Swift code using if-else statements?

hard📝 Application Q9 of 15
Swift - Control Flow
How can you simplify this Swift code using if-else statements?
let isRaining = true
if isRaining == true {
    print("Take umbrella")
} else {
    print("No umbrella needed")
}
Aif isRaining { print("Take umbrella") } else { print("No umbrella needed") }
Bif isRaining = true { print("Take umbrella") } else { print("No umbrella needed") }
Cif isRaining == false { print("Take umbrella") } else { print("No umbrella needed") }
Dif isRaining != true { print("Take umbrella") } else { print("No umbrella needed") }
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean condition simplification

    Since isRaining is a Bool, checking '== true' is redundant.
  2. Step 2: Simplify condition

    Use 'if isRaining' directly to check true value.
  3. Final Answer:

    if isRaining { print("Take umbrella") } else { print("No umbrella needed") } -> Option A
  4. Quick Check:

    Boolean conditions can be used directly [OK]
Quick Trick: Use boolean variable directly in if condition [OK]
Common Mistakes:
  • Using assignment '=' instead of '=='
  • Checking '== false' incorrectly
  • Negating condition wrongly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes