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

Richard Barquero
Richard Barquero
474 Points

Compute the sum after setting up a while loop.

I'm not too sure what my while loop is doing. Then the second part of the challenge is asking me to compute the sum? 1) Compute the sum of what? 2) and what does it mean to use the value of the counter variable as an index value?

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
}
Aaryen Singh
Aaryen Singh
1,688 Points

Hi Justin,

How exactly would I compute the sum? I am still kind of new to this.

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Richard,

Your while loop condition is set to iterate through each value in the numbers array. Since the counter variable is initialized to 0 and the numbers array contains 7 values, it will iterate from 0 to 6, making 7 total.

To answer your first question, the challenge is asking you to compute the sum of each number in the numbers array, which means in each iteration of the loop you're accumulating the sum variable with a number in the numbers array.

You use the counter variable in order to get the current index value from the numbers array. In each iteration, the counter variable is increased by one which is good for two reasons. It ensures that we don't create an infinite loop because our while condition depends on the counter eventually be greater than or equal to 7. Also, it allows us to use it against the numbers array to get the value stored by the index.

I hope this helps.

Richard Barquero
Richard Barquero
474 Points

Justin,

I re-read the code twice after reading your explanation and it dawned on me. The clarity is amazing. Good looking out.

Richard Barquero
Richard Barquero
474 Points

Justin,

will you explain why counter is placed in square brackets on the second line after numbers: sum += numbers[counter] I understand that we are addingthe numbers in the array to the total value of the variable sum with each iteration of the loop. I understand that the third line counter +=1 is incrementing the shift through the numbers array. How do I read the second line of code?

Justin Horner
Justin Horner
Treehouse Guest Teacher

Hi Richard,

I'm glad to hear it's clear now! I'd be happy to explain the line in question. The reason counter is placed between square brackets is because it is this syntax that allows us to request the value in the array stored at a particular index.

For example, given an array someArray = [1,2,3], say we want to ask the array to give us the value stored at index 2. We would use the bracket syntax like this someArray[2]. Array indexes start at 0, so the value returned from the array at index 2 would be 3.

Now if we go back to the original code, using the counter variable that gets incremented in every iteration of the loop, we can see that when counter is 0, we're passing a 0 via the bracket syntax to the numbers array to request the value stored at that element. This is the result of the code execution.

counter = 0
sum += 2

counter = 1
sum += 8

counter = 2
sum += 1

counter = 3
sum += 16


... and so on

I hope this helps