Bird
0
0

Identify the problem in this Swift code:

medium📝 Debug Q7 of 15
Swift - Control Flow
Identify the problem in this Swift code:
let number = 5
if number = 5 {
    print("Five")
} else {
    print("Not five")
}
AMissing else block
BUsing assignment '=' instead of comparison '==' in if condition
CIncorrect print syntax
DVariable 'number' not declared
Step-by-Step Solution
Solution:
  1. Step 1: Check the if condition syntax

    The condition uses '=' which assigns value, not compares.
  2. Step 2: Correct comparison operator

    Swift requires '==' to compare values in conditions.
  3. Final Answer:

    Using assignment '=' instead of comparison '==' in if condition -> Option B
  4. Quick Check:

    Use '==' for comparison in if conditions [OK]
Quick Trick: Use '==' to compare, not '=' which assigns [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Omitting else block (not an error)
  • Wrong print statement syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes