views:

460

answers:

7

Currently I am studying for my Java test. Whist studying I've come across a small problem.

In this for loop:

for ( int i=1; i <= 3 ; i++ ) {
    for (int j=1; j <= 3 ; j++ ) {
        System.out.println( i + " " + j );
    }
}

The output is:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

My problem is, I don't understand it. When I read this code I keep thinking it should look like this:

1 1
2 2
3 3

Why is this not the case?

+1  A: 

The outer for "grabs" the inner for and iterates over IT. i.e., you have a for loop within a for loop. For each of i in the range 1..3, you must loop j from 1..3 also. As i becomes 2, j resets to 1 and the inner loop runs all over again.

Stefan Kendall
+11  A: 

Each iteration of i, you're starting a completely new iteration of j.

So, you start with i==1, then j==1,2,3 in a loop. Then i==2, then j==1,2,3 in a loop, etc.

Step through it one step at a time, and it will make sense.

Reed Copsey
Oh! I get it now, thanks a lot!
에이바
Glad I could help :)
Reed Copsey
+1  A: 

You have a loop inside another loop.

So for every iteration of the outer loop, you will run the inner loop to completion, starting at 1 ending at 3.

So you end up printing j=1,2,3, for each value of i. In this case i is 1,2, and 3.

Alan
+2  A: 

What you have is one loop inside of another. Think of it like the minute hand and the hour hand on a clock. The minute hand is going to go around (1,2,3,4...12) while the hour hand is still on 1. So, if we treat hours as i, and minutes as j, we have:

1 1
1 2
1 3
...
1 60

And then the hours change to 2, and the minute hand keeps going around:
2 1
2 2
2 3
...
2 60

And we finish once we get to

12 1
12 2
12 3
...
12 60

which is where the loop ends. Your example is much like this.

(For the pedantic, I know it's zero-based, but in order to illustrate, this may be easier to understand)

Smashery
Well his loop is 1 based, so you're not far off ;)
Alan
Yeah, that's why I used 1-based, because the asker may not grasp 0-based loops.
Smashery
That was a really well thought out example. Thanks for the help.
에이바
+3  A: 

Let's look at what this would look like if we changed from loops to repeated code. First the outer loop:

for (int j=1; j <= 3 ; j++ ) {
    System.out.println( 1 + " " + j );
}
for (int j=1; j <= 3 ; j++ ) {
    System.out.println( 2 + " " + j );
}
for (int j=1; j <= 3 ; j++ ) {
    System.out.println( 3 + " " + j );
}

Now the inner loop:

// First loop
System.out.println( 1 + " " + 1 );
System.out.println( 1 + " " + 2 );
System.out.println( 1 + " " + 3 );
// Second loop
System.out.println( 2 + " " + 1 );
System.out.println( 2 + " " + 2 );
System.out.println( 2 + " " + 3 );
// Third loop
System.out.println( 3 + " " + 1 );
System.out.println( 3 + " " + 2 );
System.out.println( 3 + " " + 3 );

Does it make sense now?

Michael Myers
I understand it, thanks for the help.
에이바
+1  A: 

For each iteration of outer loop, complete inner loop will execute. For example when i= 1, inner loop will execute 3 times and you will get 1 1 1 2 1 3

Upul
+1  A: 

Every time you nest for loops (that's what it's called when you put one inside of another), it basically adds another "dimension". If you have a single for loop, it's like a straight line. So, if our first loop is from 1 to 5 it would be:
1 2 3 4 5

If you add a second loop, (lets say 1-3) (You read top to bottom left to right, first number represents the first variable, etc)

11 21 31 41 51
12 22 32 42 52
13 23 33 43 53

And if you add a third loop (let's say 1-2) (I obviously can't make 3 dimensions here, so bear with me)

111 211 311 411 511 || 112 212 312 412 512
121 221 321 421 521 || 122 222 322 422 522
131 231 331 431 531 || 132 232 332 432 532

So, the total number of iterations (how many times you go through the loops) is the product of the loops. This one is a 3 * 5 * 2, so it will iterate 30 times.

Retsam