Bird
0
0

Find the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Loops
Find the error in this Swift code snippet:
let colors = ["red", "green", "blue"]
for color in colors
    print(color)
}
AArray declaration syntax is wrong
BVariable name 'color' is invalid
CMissing opening brace '{' after for loop
Dprint statement should be outside the loop
Step-by-Step Solution
Solution:
  1. Step 1: Check for proper loop syntax

    The for-in loop requires braces '{' and '}' to enclose the loop body.
  2. Step 2: Identify missing opening brace

    The code has a closing brace '}' but no opening brace '{' after the for line.
  3. Final Answer:

    Missing opening brace '{' after for loop -> Option C
  4. Quick Check:

    for loops need braces around their body [OK]
Quick Trick: Always use { } to wrap for loop code block [OK]
Common Mistakes:
  • Forgetting opening brace
  • Misplacing braces
  • Assuming print can be outside without braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes