5

I am new at java, so there is some piece of code in nested loop I don't understand...

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

it will run: 01,02,11,12,21,22

but when I changed into this:

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

it become like this: 01,02

can anyone explain it to me?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    Even if you are new to java... the for loop construct is almost same for every language... A simple dry run would have given you the explanation – dharam Aug 22 '13 at 05:17

5 Answers5

7

The difference between the loops is that j is being reset each time in the top example but in the second example it is keeping its value.

In the top example, each time the inner for loop starts j is being reinitalized to 1 so it will go through the 1,2,3 values. When j gets to 3 it will exit the loop which is why you see the j value as 1 then 2. This is run each time the outer loop runs giving you the 0, 1 and 2 for your i values.

In the bottom example j is never reset so it will only increase. The first time through the loop it goes through the 1, 2, 3 values and exits the loop giving you the 01, 02 values you have seen. Since it does not get reset it stays as 3 so as i increases the inner loop will always exit without printing, giving you the output you are seeing.

To get the same output for the bottom example you need to reset the value to 1, which is basically what the first element of the for loop is doing.

int j = 1;
for(int i = 0; i < 3; i++) {
    j = 1; //reset the value each time through the outer loop
    for(; j < 3; j++) {
        System.out.println(i + "" + j);
    }
}
n00begon
  • 3,503
  • 3
  • 29
  • 42
6

In your first code

At case i=0

The inner loop starts with condition j=1

j=1 initialized **Condition satisfies** continue
j=2 incremented **Condition satisfies** continue
j=3 incremented **Condition Failed**    loop ends

The inner first executes it complete cycle and tend to increment of i Now i=1

Again

j=1 initialized **Condition satisfies** continue
j=2 incremented **Condition satisfies** continue
j=3 incremented **Condition Failed**    loop ends

But in your second code, j is declared outside and once j is set to 3, it remains same. So condition failed for second loop.

At case i=0

j=1 declared    **Condition satisfies** continue
j=2 incremented **Condition satisfies** continue
j=3 incremented **Condition Failed**    loop ends

At case i=1

j=3 Already set**Condition Failed**    loop ends

loop fails

Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
5

It will print 01, 02. The reason is because you don't re-initialize j. So after the second iteration of your inner loop (when i is still 0, j is 3, so it fails the condition on each of your next iterations for the outer loop.

Variables defined inside of a for-loop declaration have extremely narrow scope. This means that they die (and will need to be re-initialized) as soon as the loop finishes all of its iterations. A variable defined in the method body, as you did in the second example, will persist until the method itself concludes.

Kon
  • 10,702
  • 6
  • 41
  • 58
3

Code 1: i values are: 0,1, 2 j: 1, 2

hence output: 01 02 11 12 21 22

But in Code 2: J is initially set to 1 so in i's first iteration i.e For i=0 -> j=1, 2

but then for next iteration. j's value is not set back to 1. so j = 3. And code cant enter j's loop.

Hope thats clear enough explanation.

Rahul Dabas
  • 726
  • 2
  • 15
  • 27
3

j is not initialized in the for of the second coded block. Then the loop will start with j's current value. The first time this for is entered, j is 1, and it loops until j is 3. The second time the for is reached, j is still 3, so it doesn't satisfy the looping condition and the loop body is not executed. Following times it's the same.