What Is X After "X = X++"

What is x after x = x++?

x does get incremented. But you are assigning the old value of x back into itself.


x = x++;
  1. x++ increments x and returns its old value.
  2. x = assigns the old value back to itself.

So in the end, x gets assigned back to its initial value.

What is x after 'x += x--'?

The behavior in this case was undefined before C++17, see e.g., https://en.cppreference.com/w/cpp/language/eval_order#Undefined_behavior , so if your compiler does not conform to it, it is no use testing or trying to understand it: it will depend on implementation, or even version of the compiler.

If your compiler conforms to C++17, it is guaranteed that in a simple or compound assignment (= or e.g. +=, respectively) all of the side effects of right hand side will be dealt with before evaluating the left hand side.

In your case, x-- is evaluated to be 10 accompanied by setting x=9 as its side effect, then the computer will add 10 to x=9 resulting in x=19.

Thanks to Michał for his correction, which I incorporated into the answer.

what does x-- or x++ do here?

Just a cautionary note, sometimes the pre and post increment operators can have unexpected results

Why does this go into an infinite loop?

and what does:

x[i]=i++ + 1;

do

Read here:
http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

Is there a difference between x++ and ++x in java?

++x is called preincrement while x++ is called postincrement.

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

C++ operator+= and operator++ priority question

Warning: the assignments you are looking at have undefined behaviour. See Why are these constructs using pre and post-increment undefined behavior?
Also this answer in the question Undefined behavior and sequence points addresses how C++17 resolves these issues.

It is possible that your compiler processes the operations in the following way, explaining the differences:

pre-increment operation:
The following lines:

a=2;
a+=++a;

are equivalent too:

a=2;
tmp=++a;
a+=tmp;

which reads as:

  1. assign 2 to the variable a
  2. pre-increment a (++a) giving a the value 3 and assign it to tmp (making it 3)
  3. increment the value of a (currently 3) with the value of tmp (currently 3) giving us 6

post-increment operation:
The following lines:

a=2;
a+=a++;

are equivalent too:

a=2;
tmp=a++;
a+=tmp;

which reads as:

  1. assign 2 to the variable a
  2. post-increment a (a++) first returns the original value of a (i.e. 2) and assigns it to tmp (making it 2) and then increments a to the value of 3
  3. increment the value of a (currently 3) with the value of tmp (currently 2) giving us 5

What is (x & 1) and (x = 1)?

These are Bitwise Operators (reference).

x & 1 produces a value that is either 1 or 0, depending on the least significant bit of x: if the last bit is 1, the result of x & 1 is 1; otherwise, it is 0. This is a bitwise AND operation.

x >>= 1 means "set x to itself shifted by one bit to the right". The expression evaluates to the new value of x after the shift.

Note: The value of the most significant bit after the shift is zero for values of unsigned type. For values of signed type the most significant bit is copied from the sign bit of the value prior to shifting as part of sign extension, so the loop will never finish if x is a signed type, and the initial value is negative.

Difference in implementation of x = x + 1 and x++

My professor recently said that although x = x + 1 and x++ will obviously give the same result

I guess your professor perhaps meant - the value of x after x = x + 1 and x++ will be same. Just to re-phrase, as it seems to be creating confusion in interpreting the question.

Well, although the value of x will be same, they are different operators, and use different JVM instructions in bytecode. x + 1 uses iadd instruction, whereas x++ uses iinc instruction. Although this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation. I've checked this against javac compiler.

For eclipse compiler, from one of the comment below from @Holger:

I just tested it with my eclipse and it produced iinc for both expressions. So I found one compiler producing the same instructions

You can check the byte code using javap command. Let's consider the following class:

class Demo {
public static void main(String[] args) {
int x = 5;

x = x + 1;
System.out.println(x);

x++;
System.out.println(x);
}
}

Compile the above source file, and run the following command:

javap -c Demo

The code will be compiled to the following bytecode (just showing the main method):

 public static void main(java.lang.String[]);
Code:
0: iconst_5
1: istore_1
2: iload_1
3: iconst_1
4: iadd
5: istore_1
6: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
9: iload_1
10: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
13: iinc 1, 1
16: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
19: iload_1
20: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
23: return

Incrementing in C++ - When to use x++ or ++x?

It's not a question of preference, but of logic.

x++ increments the value of variable x after processing the current statement.

++x increments the value of variable x before processing the current statement.

So just decide on the logic you write.

x += ++i will increment i and add i+1 to x.
x += i++ will add i to x, then increment i.



Related Topics



Leave a reply



Submit