0

I am having trouble with getting why a part of code even runs:

private int m;
private int n;

public void f() {
    m = (m +2) % n;
    System.out.print(m+"\n");
}

public void g() {
    int i=0;
    m=0;
    n=8;
    while (i++<n) {
        System.out.print("i=" + i + " m=");
        f();
    }
}

If I run g(), I get the following printed out:

i++=1 m=2
i++=2 m=4
i++=3 m=6
i++=4 m=0
i++=5 m=2
i++=6 m=4
i++=7 m=6
i++=8 m=0

As I understand it, the last line shouldn't appear. Why does f() get that last time?

When f() gets called the first time, i = 0 and there for i++ = 1. (so when I print i, it is already equal to 1).

When I call f() the second to last time, i = 6, and there for i++ = 7. That is the last entire which is < n ( n = 8 ).

Why does the function get again called, when i++ is already = 8? this really confuses me.

acm
  • 2,086
  • 3
  • 16
  • 34
DavidVV
  • 225
  • 1
  • 4
  • 12
  • 1
    Do you know the difference between `i++` and `++i`? If not, research that, then you know why your code works like that. – Tom Sep 03 '16 at 09:22
  • How is it possible to receive such output? Cannot reproduce! – xenteros Sep 03 '16 at 09:30
  • @xenteros http://ideone.com/jr9QDr – Tom Sep 03 '16 at 09:34
  • @xenteros just change the print statement to: System.out.print("i++= " + i + " m=") – DavidVV Sep 03 '16 at 10:52
  • @xenteros OP just missed the "++" in his output String. The changed one from the comment fixes that. And I guess your second problem is the hardcoded `\n` and when you're on Windows, it might doesn't show a "real" line break. – Tom Sep 03 '16 at 11:08
  • In this case it didn't matter, but on [so] we often face a problem of not real output so the problem cannot be solved. – xenteros Sep 03 '16 at 11:15
  • @xenteros Even though you're generally correct, this isn't the case here. The output is logically correct and can be reproduced. – Tom Sep 03 '16 at 11:21
  • 1
    @Tom don't worry, didn't flag that – xenteros Sep 03 '16 at 11:21

4 Answers4

3

Use ++i instead of i++.

  • i++ makes a copy, increases i, and returns the copy, so it will return the old value of i (Post Increment).

  • ++i increases i, and returns i, the updated value(Pre increment).

So when you check the condition i++<n it is checked with the old value, so the condition is evaluated to true.

javaDeveloper
  • 1,403
  • 3
  • 28
  • 42
2

Try ++i instead of i++.

This has to do with the fact that:

  • ++i means "increment i and return the resulting number to the next operator to process." (PRE-increment)
  • i++ means "return the value of i to the next operation, and increment i AFTER you do so." (POST-increment)
Tom
  • 16,842
  • 17
  • 45
  • 54
BJ Black
  • 2,483
  • 9
  • 15
  • In many coding styles, it's bad practice to use `++` inside statements, as it creates confusion. (Notable exception may be array iteration with `array[i++]`) – Mark Jeronimus Sep 03 '16 at 09:27
  • Yeah, I'm not a fan of mixing up increment and compare at all either. Especially when parens would mask the difference too (if the i++ were in parens, the OP wouldn't have noticed). I get why C-like languages have this (often there is an assembly construct that's not far off), but yuck. – BJ Black Sep 03 '16 at 09:34
2

This is because i++ is post increment. That meens that it is incremented after the compare to n. You must use ++i if you want to increment i before the compare to n

Thallius
  • 2,482
  • 2
  • 18
  • 36
2

When you have:

while(i++<n){
...
}

It's as if you would have:

while(i<n){
i++;
...
}

The value first gets used, and THEN incremented.

If you want the value to first be incremented and then used in evaluation of the condition, you would just use this:

while(++i<n){
...
}

Note, that this is true not only with while, but also with if, for, do-while and any other inline evaluation of your variable.

Punga
  • 245
  • 1
  • 7