# Thread: explanation of this loop?

1. Member
Join Date
Nov 2009
Posts
7
Rep Power
0

## 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.

Java 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

2. Member
Join Date
Nov 2009
Posts
12
Rep Power
0
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.

Java 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

3. Member
Join Date
Oct 2009
Location
Rotterdam
Posts
52
Rep Power
0
Originally Posted by glopez09
Java 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:
Java 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:
Java 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.

4. 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
Last edited by aaroncarpet; 11-14-2009 at 11:43 PM. Reason: noticed pranthetical issue

5. This is the code you've posted here, I've added the closing brace '}' at the end.

Java 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.

Java 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!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•