# explanation of this loop?

Printable View

• 11-14-2009, 07:05 PM
glopez09
explanation of this loop?
hi guys. i'm having trouble understanding the flow of this loop, more specifically, nested loops in general. can somebody walk me through it? its a very small one.

Code:

```  for(int row =1; row <= 6; row++){                         for(int col = 1; col <= row; col++)                                 System.out.print(col + " ");                         System.out.println();```
it outputs this:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
• 11-14-2009, 07:26 PM
blackpanther
Quote:

Originally Posted by glopez09
hi guys. i'm having trouble understanding the flow of this loop, more specifically, nested loops in general. can somebody walk me through it? its a very small one.

Code:

```  for(int row =1; row <= 6; row++){                         for(int col = 1; col <= row; col++)                                 System.out.print(col + " ");                         System.out.println();```
it outputs this:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

1) in row 1 ->col 1-> col(o/p that is one) at end row ->2 col ->2
2) in row 2-> col 1&2 -> col(o/p is 2 & 3) col is 2 and 3(col++) at end row->3 col ->3 similar way
• 11-14-2009, 08:13 PM
Arnold
Quote:

Originally Posted by glopez09
Code:

```  for(int row =1; row <= 6; row++){                         for(int col = 1; col <= row; col++)                                 System.out.print(col + " ");                         System.out.println();```

Allright, you see this whole part:
Code:

```for(int col = 1; col <= row; col++)                 System.out.print(col + " ");         System.out.println();```
...is repeated by the first for loop (because it is wrapped in "{ }", which starts after the first loop is declared). The updates in the first for loop make sure it repeats 6 times.
At the second for loop you see no "{ }". That means only this line:
Code:

`System.out.print(col + " ");`
...will be repeated. The number of times this repeats depends on the status on the first for loop. When the first for loop is on its first cycle, the second for loop will run a cycle only once. In the second cycle (of the first for loop) the second one will give two cycles, ect.
• 11-14-2009, 11:40 PM
aaroncarpet
you have the outer loop and the inner loop the outer loop never gets reset just increments 1 2 3 4 5 6..but every time the inner loop returns to the outer loop the inner loop gets reset to 1....the second time through the outer loop is at 2 so it prints 1 2. the third time the outer loop is at 3 so its 12 3...i hope this helps

you are missing an end brace and some braces for your inner loop maybe you just quickly entered the code to give the gist of things
• 11-15-2009, 03:36 AM
Eranga
This is the code you've posted here, I've added the closing brace '}' at the end.

Code:

```for(int row =1; row <= 6; row++){           for(int col = 1; col <= row; col++)                   System.out.print(col + " ");           System.out.println(); }```
Then I've done a small change as follows to the inner nested for loop. I've wrap the code block with braces pair {}. Why I do that? It's easy to read, and also easy to understand for others, even for you in later time.

Code:

```for(int row =1; row <= 6; row++){           for(int col = 1; col <= row; col++) {                   System.out.print(col + " ");           }           System.out.println(); }```
As a practice try to do this, even your code block contains only one line of code.

You've lots of explanation about the code, so I'm not going to comment on that. Still you have something confusing in the code, let us know. Good luck!