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.

PLUS

Trouble understanding the "for" statement.

Hi guys, i'm having a bit of difficulty figuring out the concept of the "for loop". I'm still a bit unsure how it functions, so if there is any part that I'm saying it right do let me know, thanks!

I'm referring to the video "Control Flow - For Loops"

My question is what is the code that retains the previous number that was indexed out from the array from the previous pass? Which then is added to the next number pulled out from the array, giving us the 2,6(4+2),14(6+8) results during output. Here is the code as in the video:

int main() {

```int many[] = {2,4,8,10,12,14};
int sum = 0;

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

return 0;
```

}

Unless its a built in function in the "for" statement to continue adding on to the last value of "sum". From there on, I'm still quite confused I'm afraid.

Cheers

• mervin

The for loop will continue to iterate, executing the code in the body of the loop until the condition is met. Here the condition is met when the value of i is 3 or greater. Each time the loop successfully completes + 1 is added to the value of i.

What you are interested on here is this line of code: -

```sum += many[i];
```

+= is shorthand and can be thought of the same as sum is equal to the value of sum + many or: -

```sum = sum + many[i];
```

The value of i is being used as an array pointer in the many array to indicate the value contained at that numerical key. So on the 1st iteration if the value of i is equal to 0 and the value of sum is also equal to 0 then on the first iteration of sum += many[i] the value of sum is: -

```0 // sum
+
2 // The value stored in many[0]

// sum = 2;
```

On the 2nd pass, sum has been updated in the 1st iteration and the value of sum is now 2. i has also been updated (i++;) and the value of i is now 1. On the second iteration of sum += many[i], sum is equal to 2 + the value of many[1] or 4 so the result becomes 6 and so on.

The reason this works is because the variables are declared in the global scope and outside of the body of the loop, each time the loop updates a value that value is stored in the global scope ready for the next iteration to add to it.

Thanks Richard, I as well didn't get it until I read this forum

PLUS

Hi Richard, thank you so much for your detailed explanation, it all makes sense now.

So if I were to take for loops to the next level, I can use it in something in an app that calculates the weekly expanses for an individual. So in this case I will:

• need some form of input/text box that the user can input their costs from mon - sun, which
• stores it in an array......which
• gets called out from a for loop statement....
• with an initialisation of int i =0;
• and a condition of i < 7; ...
• and an increment of i++; ...
• and the rest of the code similar to the one I pasted ...

That would give me the accumulated costs with each passing day, and finally the full weeks worth of costs with the last output line.

Glad I could help :) Yes I think if you were talking about an accounting type solution you would need several components. You could store it in an array but that would only work if they added all their expenses in one job lot and would likely be lost as soon as they exit the application.

At some point you probably want to think about looking at a database or somewhere to store this information so that it can be updated on the go and reviewed in future - especially if your budget is anything like mine!

PLUS

Yeah Richard, I was thinking along the lines of something like that. but i'll need to pick up on the later modules.

I have to say that the for loop statement reminded me of a similar code block of actionscript(when I was working in a flash pipeline) that duplicates a visual asset a fixed number of times to trail the mouse cursor. Though that had some scaling involved as the duplicates reached the last few copies to emulate a tail like behaviour.

Thanks again Richard, cheers! =)

PLUS

Yeah Richard, I was thinking along the lines of something like that. but i'll need to pick up on the later modules.

I have to say that the for loop statement reminded me of a similar code block of actionscript(when I was working in a flash pipeline) that duplicates a visual asset a fixed number of times to trail the mouse cursor. Though that had some scaling involved as the duplicates reached the last few copies to emulate a tail like behaviour.

Thanks again Richard, cheers! =)