-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
  • 263,859
  • 56
  • 671
  • 702

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
  • 374,785
  • 51
  • 663
  • 734