Bird
0
0

Which of the following is the correct syntax for a safe Swift for-in loop over an array named items?

easy📝 Syntax Q3 of 15
Swift - Loops
Which of the following is the correct syntax for a safe Swift for-in loop over an array named items?
Afor item; items { print(item) }
Bfor item in items { print(item) }
Cfor (item in items) { print(item) }
Dfor item of items { print(item) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Swift's for-in loop syntax

    Swift uses 'for variable in collection { }' to loop safely over collections.
  2. Step 2: Compare options

    Only for item in items { print(item) } matches the correct Swift syntax without errors.
  3. Final Answer:

    for item in items { print(item) } -> Option B
  4. Quick Check:

    Swift for-in syntax = 'for item in collection' [OK]
Quick Trick: Use 'for item in collection' for safe loops [OK]
Common Mistakes:
  • Using parentheses like other languages
  • Using semicolons incorrectly
  • Using 'of' instead of 'in'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes