Reputation: 1031
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?
Upvotes: 8
Views: 12947
Reputation: 59673
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...59) 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)
Upvotes: 6
Reputation: 33429
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.
Upvotes: 1
Reputation: 5958
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
Upvotes: 1
Reputation: 192005
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?
Upvotes: 3
Reputation: 46873
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.
Upvotes: 1
Reputation: 564641
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.
Upvotes: 14
Reputation: 67862
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.
Upvotes: 1