# Thread: Cannot figure out basic nested for loop

1. Member
Join Date
May 2013
Posts
16
Rep Power
0

## Cannot figure out basic nested for loop

Java Code:
```for (int count = 0, count <=3; count++)
for (int count2 = 0; count2 < count; count2++)
System.out.println(count2);```
Iteration 1: count = 0, count2 = 0 , 0<0 = False->>> No output
Iteration 2: count = 1, count2 = 0, 0 < 1 = True--->>> 0
Iteration 3: count = 2, count2 = 0, 1 <2 = True -->>> 0
Iteration 4: count = 3, count2 = 1, 1 < 3 = True ->>>> 1
Iteration 5: count = Completed?, count2 = ?

I know that a 0 comes next, at some iteration, but from here I start getting confused.
Last edited by no_slip_condition; 06-06-2013 at 03:07 AM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Cannot figure out basic nested for loop

Sometimes it's best to just use a pencil and paper. Think about what the inner loop does for each value of count.
count = 0, inner loop immediately exits
count = 1, count2 = 0
count = 2, count2 = 0,
count = 2, count2 = 1
count = 3, count2 = 0
count = 3, count2 = 1
count = 3, count2 = 2

Regards,
Jim

3. Member
Join Date
May 2013
Posts
16
Rep Power
0

## Re: Cannot figure out basic nested for loop

Believe me, I've tried many times, but my fundamental understanding is flawed. Why does the outer loop pause on 2? That makes no sense to me. If you run the outer loop by itself, it just goes 0, 1, 2, 3. I also don't get why count2 reverts to 0, does that occur because the outer loop stops at 3?

4. Member
Join Date
Mar 2011
Posts
50
Rep Power
0

## Re: Cannot figure out basic nested for loop

The outer loop simply pauses while its iterating through the inner loop. Once the inner loop is finished, the outer loop increments by 1 then pauses while the inner loop goes again. and so on...

Hope that helps.

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Cannot figure out basic nested for loop

A loop simply tells whatever is within the scope of the loop a certain number of times. If that happens to be another loop, then it repeats the inner loop a certain number of times. Try the following:

Java Code:
```for (int outer = 0; outer < 5; outer++) {
System.out.println("About to start inner loop, outer = " + outer);
for (int inner = 0; inner < 5; inner++) {
System.out.println("Inner loop running, inner = " + inner);
}
System.out.println("Inner loop done, returning to next iteration of outer loop");
}```
Another way to view your original nested loops is the following:

Java Code:
```for (int count2 = 0; count2 < 0; count2++)
System.out.println(count2);

for (int count2 = 0; count2 < 1; count2++)
System.out.println(count2);

for (int count2 = 0; count2 < 2; count2++)
System.out.println(count2);

for (int count2 = 0; count2 < 3; count2++)
System.out.println(count2);```
Regards,
Jim
Last edited by jim829; 06-06-2013 at 05:20 AM.

6. Member
Join Date
May 2013
Posts
16
Rep Power
0

## Re: Cannot figure out basic nested for loop

Ahh, I get it now. Thank you very much guys. The example you posted helped a lot too.

#### Posting Permissions

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