Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Functional Programming in C Control Flow - For Loops

Zacahry Mar
Zacahry Mar
156 Points

I'm pretty confused with the lesson regarding Control Flow....I don't understand how the program ran Sum 2, 6, 14

I don't really understand the explanation in the video as to how the "Loop" was ran. I don't understand how it resulted in sum 2, 6 ,14. I think it may be because I don't understand the fundamentals of reading the equation properly

1 Answer

2, 6, 14 are the final sums, so when it first runs through the array of {2, 4, 8} 2 is the first number in the array and it is less than 3 so we do not increment (i++) the array and the sum will be 2 we now assign i=2.

The second number in the array is 4, and since it's more than 2 so we increment the value with i++ which currently equals 2, so 2+4=6 and that now that sum gets moved to i so now i=6. So now we've written sum 2 and sum 6.

Now the third part of the array the number is 8, that's also more than 3, so we are telling it to add 8 + i, which is now i=6 so we get 8+6 which is 14. So the last sum is 14.

We are incrementing i here with the number in the array each time we move to the next number in the array, we are making i the new sum.

That make sense?

Dov Breuer
Dov Breuer
8,268 Points

I re-watched this video about 4 times because i didn't get it. This helps a lot but still need to go this over a couple of times before going forward.

Thanks for the explanation.