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 Objective-C Basics (Retired) Functional Programming in C Control Flow - For Loops

Roger Green
Roger Green
643 Points

I got lost.

Okay so I followed what was being said until he ran the print statement and got 2, 6, and 14. I understand what the for statement means but i don't understand how he got the sums from the for expression.

3 Answers

Hey Roger,

In the example they use 'sum += many[i];' so as the loop runs through the array, it adds the values.

eg:

  • i = 0 sum += many[0] 2 - (value at index 0 is 2 + sum which is 0 = 2 which is now set to the value of sum)
  • i = 1 sum += many[1] 6 - (value at index 1 which is 4 + sum which is now 2 = 6)
  • i = 2 sum += many[2] 14 - (value at index 2 is 8 + sum which is 6 = 14)

I hope that helps

Roger Green
Roger Green
643 Points

You're a genius my friend thank you for your help.

Nicholas Grettler
Nicholas Grettler
1,037 Points

I was looking for a further explanation of this and this was exactly the answer I was looking for! Thank you!

No problem guys. Glad I could help.