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++)
3 for(int j = 0; j < i; j++)
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
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++)
5 for( s = r + 1; s < n; s++)
7 printf(" ");
9 for( h = 0; h <= r; h++)
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
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
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
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
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
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.