Bird
0
0

Identify the error in the following Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Operators and Expressions
Identify the error in the following Swift code snippet:
let a = 5
let b = 10
if a = b {
    print("Equal")
} else {
    print("Not Equal")
}
AUsing print instead of println
BMissing parentheses around condition
CUsing = instead of == in if condition
DVariables a and b must be var, not let
Step-by-Step Solution
Solution:
  1. Step 1: Check the if condition syntax

    The condition uses a = b, which is assignment, not comparison.
  2. Step 2: Correct operator for comparison

    To compare values, Swift requires == instead of =.
  3. Final Answer:

    Using = instead of == in if condition -> Option C
  4. Quick Check:

    Comparison needs ==, not = [OK]
Quick Trick: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking parentheses are mandatory in Swift if
  • Confusing print and println functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes