Amol Ambhore
Amol Ambhore

Reputation: 1

explanation of the nested for loop

#include <stdio.h>

int main()
{
  int i,j;
  for(i=5; i>=1;i--)
  {
    for(j=1; j<=i; j++)
    {
      printf("*");
    }
    printf("\n");
  }
  return 0;
}

Output:

*****
****
***
**
*

I wanted to know that what is the role of i and j in this code show does the loop decide what to put in rows and what in columns? Please explain the whole loop in each and every detail possible btw I am new here! Thanks!

Upvotes: 0

Views: 68

Answers (3)

John Bode
John Bode

Reputation: 123458

Let's work from the outside in.

for ( i = 5; i >= 1; i-- )
{
  loop-body
}

The sequence of operations is as follows:

  1. Initialize i to 5 (i = 5);
  2. If i >= 1 is true then goto 3, else goto 6;
  3. Execute loop-body
  4. Subtract 1 from i (i--)
  5. Goto 2;
  6. Exit loop.

So, the sequence of statements in loop_body is executed 5 times - each time the loop executes, i goes from 5, to 4, to 3, to 2, to 1, to 0. When i reaches 0, the condition i >= 1 is no longer true, and the loop exits at that point.

Breaking it down a bit more:

for ( i = 5; i >= 1; i-- )
{
  inner-loop
  printf("\n");
}

printf("\n") writes a newline character to standard output - any output following this will be written on a new line. So now our sequence is

  1. Initialize i to 5;
  2. If i >= 1 is true then goto 3, else goto 7;
  3. Execute inner-loop;
  4. Write a newline character to standard output;
  5. Subtract 1 from i;
  6. Goto 2;
  7. Exit loop.

Jumping straight to the finish:

for ( i = 5; i >= 1; i-- )
{
  for ( j = 1; j <= i; j++ )
  {
    printf("*");
  }
  printf("\n");
}

Our sequence of operations is now:

  1. Initialize i to 5;
  2. If i >= 1 is true then goto 3, else goto 10;
  3. Initialize j to 1;
  4. If j <= i is true, then goto 5, else goto 8;
  5. Write a * character to standard output;
  6. Add 1 to j (j++);
  7. Goto 4;
  8. Write a newline to standard output;
  9. Goto 2;
  10. Exit loop;

Calling printf("*"); five times in a row results in ***** being written to standard output. Calling it four times in a row results in **** being written. If you call printf("\n"); in between, you get

*****
****

Upvotes: 3

jhhoff02
jhhoff02

Reputation: 1210

When you use loops, you need a "counter", basically a variable that changes if something happens, and the loop will terminate if the counter reaches a certain number. i and j are your counters here. This is a very generic definition, you should research programming loops.

int i,j; initializes variables named i and j without giving them values.

for(i=5; i>=1;i--) says: i equals 5, while i is greater than or equal to 1, decrement i by 1 (This is what i-- means).

Within this loop is for(j=1; j<=i; j++) loop, which says: j equals 1, while j is less than or equal to i (which is originally 5, but decrements while the outer loop iterates), increment j, or increase by one (j++ means increase by one).

Each loop features a printf statement that is meant to print "*" or a new line (\n) as the outer loop and inner loop iterate.

return 0 terminates the program

Upvotes: 0

hm1912
hm1912

Reputation: 314

At first, the outer loop sets i to 5. The inner loop then repeats 5 times, as it goes from 1 to 5. Therefore ***** is printed. After the inner loop, '\n' is printed. That means we continue on the second row.
The outer loop decrements i to 4, which means the inner one is only repeated 4 times, respectively. So it prints **** and after that, the '\n' follows. We continue on the next row.
The outer loop decrements i to 3, which means the inner one is only repeated 3 times, respectively. So it prints *** and after that, the '\n' follows. This is continued until i is 1. That means, that the last loop only prints *. After that, the loop is done and the program ends.
So basically i and j are determining how often the loop is repeated.

Upvotes: 0

Related Questions