-1

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?

5
  • 2
    Can you post the code that doesn't increment as expected (i.e. the i++ one). Commented Dec 9, 2013 at 14:42
  • 4
    You only have one print statement. How are you seeing two lines? Commented Dec 9, 2013 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. Commented Dec 9, 2013 at 14:42
  • You replaced i = i + 1 with i++, literally? Commented Dec 9, 2013 at 14:43
  • Possible duplicate of post increment operator java. @whoAmI - try to avoid closing as a duplicate of a duplicate. Commented Dec 9, 2013 at 14:58

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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. 

2 Comments

But that's not what he's asking
Maybe emphasize that i=i+1 has similar behavior to ++i, as opposed to i++.
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.

Comments

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.