hello I have got my code to work but don't understand logic, I found a flow cart for how nested for loops work and with that I was able to mix things around until I got it to output correctly but while trying to read it I still haven't had that "ah-ha" moment I guess while looking at it. first time coding and first time with an online course so I feel like I've buried myself under my own questions and felt like I haven't been asking myself the right ones when trying to learn. still looking for that moment if someone could answer in question form but will most likely have follow up questions if it doesn't click after some time. if someone would be open to that thank you and if not but you still want to post an answer I understand anything helps. thanks

int s;
int h;
for (int r = 0; r < n ; r++)
   for( s = r + 1; s < n; s++)
       printf(" ");
   for( h = 0; h <= r; h++)

1 Answer 1


Don't stress yourself out too much with trying to understand everything, even after reading up on it--sometimes things only "click" after your brain stops thinking about it. Let's take a look at your loops and see if we can't get them to make a little better sense.

Instead of looking at all of the code, let's simplify the code down first. Take the following example:

1 for(int i = 0; i < 1; i++)
2 {
3    for(int j = 0; j < i; j++)
4    {
5        printf("#");
6    }
8    printf("&");
9 } 

In this example, we have one for loop with another nested inside of it. When the outer loop starts at line 1, it will try to run all code within it. The nested loop at line 3 is the first thing that gets evaluated. Unfortunately, it's condition to run (that j < i) is not met because they are both equal to 0. So, the loop skips over this and moves on to the next section of code at line 8 before repeating back to the top. When loop reaches line 3 again, i now equals 1 and the nested for loop can start.

This nested for loop will work just like the outer one did: it will move to the code inside of it and run whatever it can before repeating back to it's beginning. When the nested loop gets back to it's first line, though, and re-evaluates its condition, it sees that both j and i are equal to 1 and so it skips over all of its code and the outer loop resumes running until it also finds that it's condition is no longer met.

Now, let's look at your code, which is a more complicated example (but not by much):

1  int s;
2  int h;
3  for (int r = 0; r < n ; r++)
4  { 
5     for( s = r + 1; s < n; s++)
6     {
7         printf(" ");
8     }
9     for( h = 0; h <= r; h++)
10    {
11        printf("#");
12    }
13    printf("\n");
14 }

Your outer loop starts in line 3 and the first thing it sees is your first inner loop at line 5. Here, it will evaluate the condition for that loop. If it is true, it will execute all of the code within that loop, repeating the loop until the condition is no longer true, just as the loop did in the previous example. Once the condition is no longer true, the outer loop will move on to the next thing inside of it, which is now another loop (instead of the print statement in the last example). But, there is good news here! Nothing is different about how this loop works than the nested loop(s) that we have already looked at.

This second loop will evaluate it's condition and then loop through the code contained within its curly brackets until its condition is no longer true. Once that is the case, the outer loop will move on to the next bit of code it has. Now, at line 13, we see a printf statement just as in our previous example. The outer loop will print this whatever it is told to, then go back to the very beginning (the beginning of the outer loop) and repeat the whole process until its condition can no longer evaluate to true. These will all work the same. The trick is to not overthink them and to not get lost. :)

Hopefully that helps. If not, leave a comment and I'll try to help some more. :)

Edit: Here is an example that walks through a nested for look and how each variable's value is updated. We will use a loop similar to our first:

for(int i = 0; i <= 2; i++)
    printf("i = %i\n", i);

    for(int j = 0; j < i; i++)
        printf("j = %i\n", j);

Here is the what the above code would print out:

i = 0
i = 1
j = 0
i = 2
j = 0
j = 1

So, why is the output like that? Well, let's start at the top of the outer loop. i is initialized to 0 and this is what the first print statement prints out. Next, the program moves to the nested loop, but it doesn't run because it's condition (j < i) is not true as i and j both equal 0. Done with the code within its code block, the outer loop returns to the top of its code and starts again.

i is incremented to now equal 1 and the print statement will reflect this. Next, the inner loop is reached. j is reinitialized and set equal to 0. Because its condition (j < i) is now true, it will execute it's code block and print the value of j. Once it is done executing its code, the nested loop will return to its start and increment j so that it now equals 1. Because its condition is no longer true (both i and j are equal to 1), it will skip its code and let the outer loop resume.

Because the nested is the end of the outer loop's code block (just as before), it will return to the top of its code block and increment i so that it is equal to 2, which still evaluates to true for its condition. The print statement will now print this and the program will again continue to the nested loop, which re-initializes j to 0. This makes its condition true, once again, and so it will execute all of the code in its block. This loop prints j = 0 and then returns to its top and increments j to 1. Because its condition is still true (j < i because 1 < 2), the nested loop will run its code once again and print j = 1. Once it returns to its top and increments j once more, though, its condition no longer evaluates to true and so it skips its code and lets the outer loop continue running.

At this point, the outer loop has no code left to run, so it returns to its top and increments i to 3. Because this makes the outer loop's condition no longer true (i is no longer less than or equal to 2), the outer loop skips its code block and lets the program continue running the code below it.

  • Thank you! this was very helpful. honestly was getting hung up on just the regular use of plus/minus within the code and how that fit when the code is updating. Trying to keep track of values was making more questions on how that function worked instead of moving forward getting my code to work.
    – troycramer
    Commented Jan 17, 2021 at 23:22
  • @troycramer I'm glad I could help. Does it all make sense, or could you use an example/explanation of how the values are getting updated? I could always update my answer with another example to show this. Commented Jan 17, 2021 at 23:29
  • sorry for the long silence, but honestly yes if you have the time to do that( show how they are being updated) that would be greatly appreciated! not going to say no to learning if you are offering haha
    – troycramer
    Commented Jan 21, 2021 at 6:16
  • @troycramer I added an edit at the end of the post with a new example that walks through how the variables are incremented through the loops. Let me know if you have other questions and don't forget to accept the answer if it has helped so the system knows the issue is resolved. Commented Jan 21, 2021 at 16:32
  • thank you again! i haven't had time to work on anything new or spend much time at all working with this but this answered my questions completely! reading through while looking at my code it does make sense know and I don't feel hung up on it anymore.
    – troycramer
    Commented Jan 27, 2021 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .