samedi 16 mai 2015

What's the difference between i++ vs i=i+1 in an if statement?

For the 1st code,

int i = 1;
while (i < 10)
if ((i++) % 2 == 0)
System.out.println(i);

The system outputs: 3 5 7 9

For the 2nd code,

int i = 1;
while (i < 10)
if ((i=i+1) % 2 == 0)
System.out.println(i);

The system outputs: 2 4 6 8 10

Why are the two outputs different but the formula is the same?

Aucun commentaire:

Enregistrer un commentaire