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

For Loops Video Help

Hey guys,

So I've watched the Control Flow - For Loops video a dozen or so times, writing it down, typing it up, and generally trying to make sense of it. I feel like I'm on the cusp of making sense of it all but there is just one big part I'm missing.

This is the code:

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

Here is the transcript when he explains the summations.

The sum initially starts at 2 when "i" was 0;

  • I understand this.

"i" then is auto incremented to 1.

  • I understand this.

When we indexed the array "many" many sub1 is 4

  • Got it.

2 + 4 is 6. -THIS, I don't understand where does adding 2 + 4 come in? is this Sub1 and Sub2 of the array?

Any help would be useful.

5 Answers

20,306 Points

Hi Jason, 'many' is an array containing the values: {2, 4, 8}. the "for loop" runs three times, and on each iteration, sum is increased by the many[i] which is value at the current index, depending on the iteration. So if i = 0 (first iteration), then many[0] is the first item on the many[] array, which is 2. when i = 1, many[1] = 4, and when i = 2, many[2] = 8. each time the iteration runs, sum is incremented by many[i]. It's the same as saying: sum = sum + many[i];

so before the first iteration, sum = 0; On the first iteration: sum = sum + 2 (2) On the second iteration: sum = sum + 4 (2 + 4 = 6) On the third iteration: sum = sum + 8 (6 + 8 = 14)

Remember that sum's value is added through the loop.

I seriously had to read that more than a few times to get it but then it clicked. You said "The sum's value is added through the loop," which means with each iteration we're adding the sum of the last iteration to many and that's how "sum + 4" is really 2 + 4 which equals 6, and on the third iteration it's 6 + 8.

Wow, thank you so much for that.

Okay and one more question. In my original post I setup my three backticks before and after my code to post it properly but it's still off. What am I doing wrong?

It looks like you posted inline code with a single backtick.

When using the 3 backticks you need to have a blank line before the first set of backticks. Then the 3 backticks on their own line with your code following on the next line. Then end it with 3 backticks on it's own line.

Here's a link with more info: https://teamtreehouse.com/forum/how-to-type-code-in-the-forum

Got it now, thank you sir.