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

control flow help

Hi, I'm really confused about control flow for c programming. I don't really understand what it is and how it works. I've watched the control flow - for loops video and don't understand the loops part. Help would be highly appreciated

2 Answers

Lachlan Mitchell
Lachlan Mitchell
9,278 Points

Hey Daniel!

Let's go through it line-by-line. In the lesson, the following loop is explored:

int main()
{
    // Array defined.
    int many[] = { 2, 4, 8 };
    // Integer defined.
    int sum = 0;
    // For as long as i is less than 3, run the following
    // code and then increase i by 1.
    for (int i = 0; i < 3; i++) {
        sum += many[i];
        printf(sum %d\n, sum);
    }
    return 0;
}

In programming, a loop is a construct that allows you to run one or more lines of code over and over again until a desired outcome is reached. In the above case, we're looping three times through the code in the for loop block (the indented area wrapped in curly braces).

We begin the for loop. Notice that in the parenthesis (brackets) after the for loop, there are three statements separated by semicolons. The first statement defines an integer, i, as 0. The second statement is a conditional, i < 3. The third statement is a convenience operator that increments the value of i by 1.

Following these three statements is the main body of the for loop. To start with, we initialise that variable, i. You can think of i as standing for index, and its value corresponds to the number of times we've already looped through the code. Next, the conditional (second statement in the parenthesis) is going to be evaluated. If it evaluates to true, we will run the code inside the for loop and then increment the value of i by one (as defined in the third statement).

So how's this conditional statement going to work? Well the first time we run it, i will be equal to 0.

  • 0 < 3 is true, so the code in the loop will run once (the number at index i in the many array will be added to the variable sum and printed to the screen). After this, i is incremented by 1. Then the conditional is checked again.
  • 1 < 3 is still true, so the code in the loop will run again, and once again i will be incremented by 1.
  • 2 < 3 is still true, so it will happen again.
  • 3 < 3 is false, because 3 is not less than 3. This time, we will break out of the for loop and move past it to the return statement.

If it helps, you could try thinking of it like this:

// We want to buy 10 bananas, we currently have 0...
for (int bananas = 0; bananas < 10; bananas++) {
    getABananaOffTheShelf();
}
// Now we have them!

It's a complicated concept until you get the hang of it, but if you keep practicing I promise you'll understand it soon. Hope that all helps!

  • Akyri

Thanks soooo much Lachlan i get the hang of it now and you explained it very well. :)

Zacahry Mar
Zacahry Mar
156 Points

I don't understand how the results are sum 2 ,6, and 14 respectively.....