3
import java.io.*;
public class test {
    public static void main(String args[]) {
        int a=0, b=6, sum;
        for(int i=0; i<=2; i++) {
            System.out.println(i=i++);
        }
    }
}

Output: 0 | 1 | 2. But actually I think it should be 0 | 2. Please explain why I am wrong? Thank you in advance.

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
ashley
  • 57
  • 5

7 Answers7

3

The difference is in this line of code:

System.out.println(i=i++);

i++ is a post increment, meaning it is only executed after the rest of the statement.

so, it goes a bit like this:

System.out.println(
  int tempI = i;
  i = tempI;
  tempI = i + 1;
  );

In the end, you print the value of i, while the value of tempI is not used after that, so considered lost.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • That's wrong interpretation. Can you try executing this `{ i=i++; System.out.println(i); }` ? – Suresh Atta Sep 02 '15 at 07:14
  • Suresh: it's still the same. only, here the i will be augmented BEFORE he print statement is reached. I used tempI as an example of the logic, not of how the actual code is. – Stultuske Sep 02 '15 at 07:15
1

Answer is in byte code generated for above code. It store back the old value of i into i. Therefore i=i++ statement make no impact logically.

iconst_0
istore_1
goto 11
getstatic java/lang/System/out Ljava/io/PrintStream;
iload_1
iinc 1 1
dup
istore_1
invokevirtual java/io/PrintStream/println(I)V
iinc 1 1
iload_1
iconst_2
if_icmple 4
return
mohit kumar
  • 179
  • 2
  • 10
0

In your code:

for(int i=0;i<=2;i++)
{
  System.out.println(i=i++);  //here the value of i is incremented after it is assigned to i.
}

When you are doing i=i++ then i is incremented after the assignment. And hence you are not getting as expected.

You can use pre-increment operator and see the difference.

for(int i=0;i<=2;i++)
{
   System.out.println(i=++i);  
}

Here is a related thread which will help you: What is x after “x = x++”?

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

It is easy to test it out by yourself actually even without using a debugger.

    int a=0,b=6, sum;
    for(int i=0;i<=2;i++)
    {
        System.out.println(i=i++);
        System.out.println("Value of i:" + i);
    }

OUTPUT:

0
Value of i:0
1
Value of i:1
2
Value of i:2

Question: So why after System.out.println(i=i++);, value of i is not increasing?

Answer: This is because i++ means post-increment. The i at the right hand side will only increase by one after that line.

//Let say i is 0.
i = i++; 

i++ means the right hand side i will still be 0 till it goes to next line. Hence 0 was assigned to the i at the left hand side.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • then in SOP(i=i++), value of i should increase after this line. so after compiler printing this, it then goes to for loop increment, so here the 'i' must have incremented it right? and, then again i increment it in for loop, so basically it is incrementing it twice, i guess. – ashley Sep 02 '15 at 07:33
  • @Ashley Your statement is true only if you use a different variable on the RHS. Example: `i + b++`. You print `b` after that it will give you 7. But If you use the same variable on LHS and RHS (i.e. `i = i++`), you assigned the old value back to itself. After that, the i++ is disregarded. This is how Java designed it for post increment. Different from C++. – user3437460 Sep 02 '15 at 07:39
0

If this works:.

for(int i=0;i<=2;i++)
{
    System.out.println(i=++i);
} 

Because this does not work?:

for(int i=0;i<=2;i++)
{
    System.out.println(i=i++);
} 

Apparently the postincrement in the println method is treated differently.

SkyMaster
  • 1,323
  • 1
  • 8
  • 15
0

Please see this snippet:

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

this should give you a short way to do it.

JBALI
  • 89
  • 6
0

It is an undefined behaviour in C or C++ if you write:

i = i++;

Reason being that the order of evaluation cannot be determined. Hence if you write this in C or C++, there is no guarantee what will be produced.

In Java, this kind of ambiguity is removed from the design. The order of evaluation is as follows:

i = i++;    //value of i++ is stored (0 is stored in this case)
            //i increased by 1, i is now 1
            //The stored value assigned back to i (0 assigned to i)
user3437460
  • 17,253
  • 15
  • 58
  • 106