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 2.0 Collections and Control Flow Control Flow With Loops Working with Loops

Sorry i don't get this one..

Sorry i don't get this one.. The loops are very difficult i think.

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

// Enter your code below

2 Answers

Andres Aguero
Andres Aguero
30,545 Points

Hey Lucas,

Don't worry loops were very difficult for me when I started, but with a little practice they become a force of nature.

Step 1: You need to define how many numbers are in the array and you do this with the count property.

while counter < numbers.count
{
    counter++
}

Thank you Andres!

and can you also help with step 2?

Lucas.

Andres Aguero
Andres Aguero
30,545 Points

Hey Lucas,

The step 2 is simple. All you have to do is add the values together to get the sum of all of them. You can do it by extracting each value from the array*(the counter variable)* and adding it to the sum variable.

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