Bird
0
0

The following Swift code has an error. What is the problem?

medium📝 Debug Q14 of 15
Swift - Operators and Expressions
The following Swift code has an error. What is the problem?
let range = 1..5
for num in range {
    print(num)
}
AThe range operator .. is invalid in Swift
BThe loop variable num is not declared
CThe print statement syntax is incorrect
DThe range should start from 0
Step-by-Step Solution
Solution:
  1. Step 1: Check the range operator used

    The code uses 1..5, which is not a valid range operator in Swift.
  2. Step 2: Identify correct range operators

    Swift uses ... for closed ranges and ..< for half-open ranges, but not ...
  3. Final Answer:

    The range operator .. is invalid in Swift -> Option A
  4. Quick Check:

    Invalid operator .. causes error [OK]
Quick Trick: Use ... or ..<, never .. alone [OK]
Common Mistakes:
  • Using .. instead of ... or ..<
  • Assuming .. is a valid operator
  • Ignoring compiler error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes