-3

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.

CodeCaptain
  • 379
  • 4
  • 10
  • nope, incorrect. the result of the `i++` expression is the value of `i` before it is incremented. So if `i` starts as `1` it will be incremented to `2` but `i++` results in `1` and `1 % 2` is `1` - `if` block is not executed; on second iteration `i` is `2` and incremented to `3` but `i++` results in `2` and `2 % 2` is zero - if block is executed and prints `i` which is `3` – user85421 Oct 06 '19 at 00:08

3 Answers3

1

The answer is because when i = 1, i++ % 2 == 0 returns false, so nothing is printed. This is because 1 % 2 == 1, not 0.

The i in the expression (i++) % 2 == 0 is used prior to i being incremented.

So when i is 2, i++ % 2 == 0 is true. But after that, i has been incremented to 3 so it prints 3.

It continues on in this fashion, evaluating to true when i, before incrementing, is even.

Here is a simpler example.

      int k = 0;
      if (k++ == 0) {
         System.out.println(k); // print 1
      }

On the other hand, if it was pre-incremented, it would work like this.

  int k = 0;
  if (++k == 1) {
     System.out.println(k); // print 1
  }

Note the following:

int i = 1;
System.out.println(i++ == i);  //Prints false

This is because the expression evaluates as 1 == 2 since the pre-increment value of i is used first but then uses the incremented value for the target of the compare. Thanks to Carlos Heuberger for pointing this out.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    not fully correct: "The expression `(i++) % 2 == 0` is evaluated prior to `i` being incremented.No, it is not - " `i` is incremented as part of the evaluation of `i++`, the result of `i++` is the value of `i` before increment (if `i` were used elsewhere in the expression, that could be relevant) – user85421 Oct 06 '19 at 01:07
  • @CarlosHeuberger Thanks for pointing this out. I corrected my answer and gave another example showing this. – WJS Oct 06 '19 at 01:39
1

Think of i++ as a method call, let's call it returnThenIncrement(i).

Think of ++i as a method call, let's call it incrementThenReturn(i).

In your code if i=0 and you "returnThenIncrement(i)" you would get 0 and THEN i would be incremented for the next statement.

In your code if i=0 and you "incrementThenReturn(i)" you would get 1 because i was incremented before it was used.

fedup
  • 1,209
  • 11
  • 26
1

Your program will never print 1 because you fail on the first step with i=1:

if( 1 % 2 == 0) // Evaluates to false
System.out.println(2); // Code is never reached

If you want it to print 1 change first line to int i=0; and your algorithm evaluates something like:

if( 0 % 2 == 0) // Evaluates to true, and i is now 1
System.out.println(1); // Eureka
Beto Rodriguez
  • 114
  • 1
  • 3