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

Control Flow - For Loops

I have watch this video over and over, and I am not understanding how he is getting the different sums. How is the program going from sum two to sum six? Sadly i can not copy the code.

can you post the link to the video

8 Answers

ok here is the looping code

int many[] = {2, 4, 8};
int sum = 0;

//set i equal to 0, as long as i is less than 3, perform the loop body and add 1 to i
for (int i = 0; i < 3; i ++) {

    //add the value of the array at index i to the variable sum
    sum += many[i]    
    printf("%d", sum);

Ill explain in psuedocode-like language

First time the loop runs:
i is 0, which is less than 3. Perform the loop

Sum is 0 right now
 execute the code sum += many[i] (remember i is 0)

many[0] is the value 2 so sum = 0 + 2  which is 2.

2 will be printed out

increment i by 1
second time the loop runs:
i is now 1, which is less than 3, perform the loop

Sum is 2 now
execute the code sum += many[i] (remember i is 1 now)

many[1] is the value 4 so sum = 2 + 4 which is 6

6 will be printed out

increment i by 1
third time the loop runs:
i is 2 which is less than 3, perform the loop

Sum is 6 now
execute the code sum += many[i] (remember i is 2 now)

many[2] is the value 8 so sum = 6 + 8 which is 14

14 will be printed out

increment i by 1
4th time:
i is 3 which is not less than 3, do not perform the loop

**int main() {

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;

}**

Above is the code he was using that I am thinking you are referencing. I will breakdown each line of this code to hopefully show you exactly what is happening in what order.

1) We start our main function by setting up an Array of numbers called many many contains 3 values that are indexed by the values 0,1,2 this means that many[0] = 2, many[1] = 4, and many[2] = 8

2) We set up an integer called sum and set it to 0

3) We begin creating our for loop for loops are passed three arguments before they start 1- The integer that will be incremented (in this case the variable "i") 2- The integer's max value that will tell us when to stop the loop (in this case, once i is no longer less than 3) 3- How we are going to increment i (in this case, we use the increment operator that adds 1 to i every time the loop repeats

4) i = 0 which is less than 3 so the loop will run the first command of the loop sets the integer "sum" equal to itself + main[0] (recall that the += operator means add the current value to this value) we know that sum is currently 0 and main[0] = 2 so 0+2=2 the value of sum will be printed (sum = 2)

5) We start at the beginning of the for loop, i = 1 because it was incremented by the i++ command and since it is still less than 3, we run the loop again the first command adds the previous value of sum (which is now 2) to main[i] and i =1 so main[1] = 4 2+4= 6, the new value of sum the new value of sum is then printed (sum = 6)

6) We return to the beginning of the loop, i = 2 because it was incremented by the i++ command and since it is still less than 3, we run the loop again the first command adds the previous value of sum (which is now 6) to main[i] and i = 2 so main[2] = 8 6+8= 14, the new value of sum the new value of sum is then printed (sum = 14)

7) We return to the beginning of the loop, but now i = 3 which is NOT less than 3, so our loop will not run again

8) Our code is complete

Hope this helps!

Ohh ok I get it now, what was trowing me off was the i++, is that just to move down the list of arrays?

yes in this case i was used as the index of the array, so in order to move through the array values you have to increment it by 1 each time.

Ok great, Thanks for the help !

no problem

Sorry, I was still typing my response as your answers were being posted =)

happens to me a lot to.

Thanks for help too Josh :P

Holy smokes! Thanks for the help. This was still blowing my mind after a few watches. The explanations helped greatly.