My brother is taking a Java class. I am not all that familiar with Java but I know C#. I am trying to explain why the following code returns 3,5,7,9 instead of 1,3,5,7,9. I said that the new (incremented) value of i is not available in the line where i gets incremented so the if condition is not satisfied when i is 1. I'm not sure that i am explaining it well so I'm hoping perhaps someone can give a better explanation. Why does the code return 3,5,7,9 instead of 1,3,5,7,9?
int i = 1;
while (i < 10)
if ((i++) % 2 == 0)
System.out.println(i);
Really Sotirios??? This is a duplicate? This question is different from the other question because this question includes a loop. Also, the increment happens in the if statement. I'll admit that the other question is informative and similar in context but that does not make this a duplicate. Someone who is new to Java could learn from this question and not learn from the other (or vice versa). Sometimes it feels as if the standards for duplicate questions are arbitrary.