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

Jim Brodhagen
Jim Brodhagen
1,081 Points

Stuck on While Loop lesson

I've been stuck on this lesson for days. I've watched the preceding videos 5-6 times each and I'm still at a loss for how to proceed. I'm sure the answer is simple, but I just can't wrap my head around it.

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

I understand that something needs to go into the while loop BEFORE the counter is incremented up. But for the life of me I can't seem to understand how to write this. Any help?

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

// Enter your code below

while counter < numbers.count {

2 Answers

28,558 Points

You are correct, you are literally just missing one line of code that goes in the beginning of the loop, to help you out I'll first post said line and then explain how it works:

sum += numbers[counter]

Let's start with what is on the right of the +=, when you have a variable holding an array of items you can retrieve single items out of it by accessing the item using it's index. So for example the line "numbers[0]" would retrieve the item at index 0 (the first item), "numbers[1]" would retrieve the item at index 1 (the second item) and so on.

If you use the counter as the index value then you will retrieve the first item during the first loop, the second during the second loop and so on.

the += symbol is essentially just a shortcut for this expression: "sum = sum + numbers[counter]", in other words you are taking the value currently in sum and adding the value of "numbers[counter]" to it.

So in the first loop sum will be 0 and have 2 added to it (2 being the first item in the numbers array) then during the second loop sum will be 2 and have 8 added to it and so on.

By the time the loop has finished you will have added all of the numbers of the numbers array to the sum variable, which means that you have effectively calculated the sum of the list.

If you still have questions about this or found my explanation unclear, then please feel free to ask followup questions. I'll be more than happy to clarify anything you are struggling with.

Jim Brodhagen
Jim Brodhagen
1,081 Points

This is a very clear explanation. Many thanks for the effort and consideration! This makes perfect sense to simply use the variable "counter" in the where loop so it iterates through the entire array. Again, thank you. This clarified the issue/solution quite well.