0

I have gone through all the questions in this forum that are corresponding to this topic, and I felt to raise a different question as this question is not answered clearly. Here is my scenario: I have this class Test1:

public class Test1 {
    public static void main(String args[]) { 
        int i=0, j=2;
        do {
            i= i+1;
            j--;
        } while(j>0);
        System.out.println(i);
    }
}

Now this is my question: a) If I replace the increment operation of 'i= i+1' with ++i, I get the desired output as

1
2

But if I replace the increment operation of 'i= i+1' with i++, I get the desired output as

0
0

I do understand the difference of using prefix and postfix operators in for loop, but why is the value not getting incremented at all in do-while loop?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Shubhankar Raj
  • 311
  • 1
  • 10
  • 2
    Can you post the code that doesn't increment as expected (i.e. the i++ one). – Tim B Dec 09 '13 at 14:42
  • 4
    You only have one print statement. How are you seeing two lines? – Sotirios Delimanolis Dec 09 '13 at 14:42
  • 1
    This has nothing to do with the do-while loop and is probably because you've changed `i = i + 1;` to `i = i++;`. If you don't know what's wrong with that you should go back and read all those questions again. – Radiodef Dec 09 '13 at 14:42
  • You replaced `i = i + 1` with `i++`, literally? – Dave Newton Dec 09 '13 at 14:43
  • Possible duplicate of [post increment operator java](http://stackoverflow.com/questions/2750216/post-increment-operator-java). @whoAmI - try to avoid closing as a duplicate of a duplicate. – Duncan Jones Dec 09 '13 at 14:58

4 Answers4

5

Using i = i++; will never change i value.

Use just:

i++;

Why? because:

i = i++;

is similar to doing something like this:

temp = i;  // temp is 0.
i = i+1;  // increment i
i = temp;   // assign temp (which is 0) to i.

Take a look at a similar post-increament question and even another one.

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2
++i

This will increment i, then return it.

i++

This will return i, then increment it.

Example

Here is a link to an ideone example of this.

int i = 5;
System.out.println(i++);

// Outputs 5

int j = 5;
System.out.println(++j);

// Outputs 6.
christopher
  • 26,815
  • 5
  • 55
  • 89
1

x = i++; is a shortcut for x = i; i = i + 1;

x = ++i; is a shortcut for i = i + 1; x = i;

that's it.

V-X
  • 2,979
  • 18
  • 28
0

Actually in your sample, there should be no difference between i++ and ++i, if you replace i = i+1 by one of two. Chris noted the difference correctly, I'll just add a sample, where this is important:

int i = 0;
method(i++);

Will call method with the original value of 0 and increment i afterwords. In method, however, you'll see the original value 0.

int i = 0;
method(++i);

Will increment i and then call method. So in method you'll get the value of 1. That does not change the fact that after method, i has the value 1 in both cases.


Others have noted correctly that

i = ++i;

and

i = i++;

are different things actually!

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139