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

Taj Patel
Taj Patel
3,749 Points

I don't understand what the purpose of many[i] is.

I see that sum = 0 and i = 0, but when they put many[i], I don't understand what that correlates to. Isn't i independent of the many[] function?

2 Answers

Arman Kussainov
Arman Kussainov
4,754 Points

We use many[i] when looping through an array where i - is the index.

Kevin McFarland
Kevin McFarland
5,122 Points

HI Taj Patel,

You've most likely have figured this out by now... if so, please forgive me.

in the statement:

               int many[] = {2, 4, 8};
               int sum = 0;
               for (int i = 0; i < 3; i++) {
                     sum += many[i];
                     printf("sum = %d\n", sum);
}

Remember that the lesson is on loops and that the word "for" begins the process with a set of operators (.i.e = < ++). The first time through the var "i" which is an integer is initialized to a value of 0.

That initialization takes place only once - it is then compared to a value of 3 with the less than symbol "<". If it is less than 3 - which it is the first time through because it's been initialized to 0 then the integer "i" is auto incremented by a value of 1 with the "i++" operator.

Since it is less than 3 the programs enters the loop which is between the curly braces...

 beginning of  loop  - {

{ sum += many[i]; printf("sum = %d\n", sum);

  end of loop - }

The var "sum" is then assigned the value of the "many[i]" array. Remember [i] represents a holding place in the array. In this case either location 0, 1 or 2 which correspond to the number 2, 4, 8 respectively.

Since "sum' = 0 and "i" now = 1 (because it's been auto incremented by "i++") the var "sum" now = 1 and so on until "i" is equal to 3. At which time the program exists the loop.

So the 1st time through "sum" equals itself plus the value that 'i' represents ( because of the operator += )

So "sum" (which equals 0) + [i] ( which equals 2) now equals 0 + 2

2nd time "sum"(which now equals 2 ) + the second number in the array( which equals 4) now is set to 2 + 4 or the value of 6

3rd time "sum" = 6 + [i] ( the third value in the array which is 8 ) or 6 + 8 = 14

I hope that didn't over complicate the matter... if nothing else it help clarify things for myself.

Best of luck!

Kevin McFarland