-3
class Test 
{
    public static void main(String args[])
     {
         int i=1;
         for(int j=0;j<=2;j++)
             {
                 i=i++;
             }
          System.out.println(i);
      }
}

Why in this question the value of i is printing 1.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

2

i=i++; doesn't change i.
It increments i but then assigns the old value of i to i (since the post increment operator returns the old value of the incremented number).

Either write :

i++;

or

i=i+1;

Eran
  • 387,369
  • 54
  • 702
  • 768