Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

stephenallison
stephenallison
8,559 Points

Stuck on Swift 3 Collections and Control Flow challenge task 2

I've changed my code multiple times but i continue to get the same error. I need simple explanations because apparently I'm not understanding Swift

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter < numbers.count { 
  sum += numbers[counter] counter += 1 
}

1 Answer

Hi Stephen,

Your logic is absolutely fine but there is just a small syntax mistake. If you want to use multiple statements in a single line, you should separate them with a semicolon. As in your case, the two statements in the while loop should be separated by a semicolon because if you don't, the compiler would treat them as a single statement leading to an error.

You should always try to align your code properly as it improves readability and helps you to determine where the problem is.

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter < numbers.count { 
  sum += numbers[counter]; counter += 1 
}

Hope this helps :) Happy learning!!!

stephenallison
stephenallison
8,559 Points

Thank you kasaltrix for the explanation. Its amazing how a little, simple syntax error can drive someone crazy!!