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

Matthew Lurie
Matthew Lurie
3,512 Points

Lost. "Using the value of counter as an index value..." That plus the rest of the sentence I am totally lost on.

"Using the value of counter as an index value, retrieve each value from the array and add it to the value of sum."

Don't know what I'm doing.

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 {
    counter += 1
    sum 
}

4 Answers

Ben Reynolds
Ben Reynolds
35,170 Points

sum = sum + numbers[counter]

That looks right (i think sum += numbers[counter] would also work). Can you share the full code for your loop so I can see it in context? Make sure you're adding to sum before you increment the counter.

Could we have written "var banana = 0"

Totally. "counter" is not a keyword, just a common name for a loop counter. Most of the time you'll probably see people just use the letter "i" which stands for index or increment depending who you ask.

Ben Reynolds
Ben Reynolds
35,170 Points

We have an array called numbers, and every item in it has an index. The first index is 0 with a value of 2, the second is 1 with a value of 8 and so on.

To grab an item from the array we use that index, so to get the first number it would be numbers[0] which would return 2

In the loop, we have a counter that also starts at 0, so you can get the first thing in the array like this:

numbers[counter]

Since counter happens to be 0 at this stage, numbers[counter] will equal 2, which can be added to "sum".

At the end of each loop iteration the counter should go up by 1, so the second time the loop runs, numbers[counter] would be the same as numbers[1] and so on until the loop condition becomes false and the loop stops.

Matthew Lurie
Matthew Lurie
3,512 Points

Ok tried this and it isn't working: sum = sum + numbers[counter]

I'm following what you wrote. Just not following how to get from what you wrote to "retrieve each value from the array add it to the value of sum."

Retrieve each value: numbers[counter]. That just gets me whichever value the numbers array happens to be on - so it gets me 2, 8, 1...

How do you add up each one? I guess that's what I'm confused about.

Separately, why does "counter" get us the index - is that an official word for figuring out which part of an index you're on? Could we have written "var banana = 0" and Swift would have allowed us to use that to find the index of the array?

Matthew Lurie
Matthew Lurie
3,512 Points

ok got it!

The issue was that I wasn't putting it before I incremented the counter!

Which leads me to: Why does it matter if I put it before or after?

Thanks for the clarification on "counter" - it's always tricky to remember what is official keywords ("var" "print") and what is just the common parlance.

Ben Reynolds
Ben Reynolds
35,170 Points

The reason it has to go after is that incrementing the counter prepares it for the next loop iteration, not the current one. So if counter = 0, and then you bump it up to 1 right away before the sum gets updated, then this line...

sum += numbers[counter]

...would be equivalent to sum += numbers[1] during the very first pass. Index 0 would get skipped altogether.

Matthew Lurie
Matthew Lurie
3,512 Points

just barely getting it...:) thanks for your help!