What is x after x = x++?
x
does get incremented. But you are assigning the old value of x
back into itself.
x = x++;
x++
incrementsx
and returns its old value.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:
- assign
2
to the variablea
- pre-increment
a
(++a
) givinga
the value3
and assign it totmp
(making it3
) - increment the value of
a
(currently3
) with the value oftmp
(currently3
) giving us6
post-increment operation:
The following lines:
a=2;
a+=a++;
are equivalent too:
a=2;
tmp=a++;
a+=tmp;
which reads as:
- assign
2
to the variablea
- post-increment
a
(a++
) first returns the original value ofa
(i.e.2
) and assigns it totmp
(making it2
) and then incrementsa
to the value of3
- increment the value of
a
(currently3
) with the value oftmp
(currently2
) giving us5
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
How to Get the User Input in Java
Can't Execute Jar- File: "No Main Manifest Attribute"
Java 256-Bit Aes Password-Based Encryption
Is There an Eval() Function in Java
How to Count the Number of Occurrences of a Char in a String
Floating Point Arithmetic Not Producing Exact Results
How to Clone Arraylist and Also Clone Its Contents
How to Get the Current Working Directory in Java
How to Use the Tostring Method in Java
Why Is Subtracting These Two Times (In 1927) Giving a Strange Result
How to Set Environment Variables from Java
Why Doesn't Java Allow Overriding of Static Methods
How to Pad an Integer With Zeros on the Left
Why Is Using a Wild Card With a Java Import Statement Bad
What Is a Stack Trace, and How to Use It to Debug My Application Errors