Increment and Decrement operators in Java and C++?
For this particular question. int x=0; cout<<(++x + --x) Gives '0' as the answer. in C++. Whereas in Java.. System.out.println (++x + --x) Gives '1' as the answer. Why?
@Victor In C++ if two or more operators with same precedence appears in an expression in a same line then the order of evaluation of operands is undefined. The result you may get is totally dependent on compiler specific implementation. In the above C++ case ++x + --x; while trying to evaluate the operands (hence expression), the compiler may choose to evaluate ++x first and then --x or it may choose to evaluate --x first and then ++x or it may choose to do something else altogether. Hence the result is either undefined or compiler dependent. There are tons of C++ compilers available and I am sure that the results will not be consistent across all of them.
@Abhishek, I didn't get about C++? There is no postfix operator used!
multiple usage of increment operators on a single line and a single variable will result in undefined behaviour. basically it is unknown as to which order the compiler will chose to complete the expression.
@Abhishek, You mean, x=0; ++x means 1, and then --x means -1 (which must be 0), and then 1 and -1 (which supposed to be 0) will cancel out and output 0? I'm satisfied with NeutronStar
that's y all prefix forms are evaluated first.. first increment increases x by 1 but then decrement nullifies the effect and then the final result is put into expression which is 0+0
Nullifies? Java does not nullifies but simply decrements by 1, when use --x; Why C++ nullifies?
in java if an expression doesn't have parenthesis then it is always evaluated from left to right keeping in mind the operator precedence of operators encountered. here ++x is encountered first so it results in 1 and then --x results in 0 hence addition is 1... in c++ precedence of prefix operators is always greater than postfix versions.. hence ++x results in 1 but --x results in 0 so ultimately 0 will be kept for x in expression
@Victor, Thank you for marking my answer as best : ) Sorry it was due for quite sometime.
@Sachin by nullify I mean that both postfix and prefix versions occur one after the other hence there's No effect
@sachin also if u read operator precedence of java then prefix and postfix have same precedence and in such an expression evaluation always takes from left to right
here the evaluation is 0 + 0 in c++ which results in zero. if you consider this code int a=0; cout<<(++a + ++a +a++); it will print 6 remember in c++, the evaluation is from right to left. here the evaluation is 3+3+0 that is pre-increment takes the final value and post-increment takes the value at that instant and then increments it. but in Java, the answer is 5. Here in Java, the evaluation is done from left to right here the evaluation is 1+2+2 that is pre-increment first increments the value and then takes the value and post-increment first takes the value and then increments it. the main difference in java and c++ 1)direction in which the evaluation is done. 2)pre-increment and pre-decrement operators.In c++, PI and PD take the final value of the variable and in Java, PI and PD take the value at that instant.
@Abhishek In C++ postfix operators have higher precedence than prefix operators.
Python — File Handling
Help me solve this (using loop)
What is wrong? Error on test.
Help me wiht python
Question is write a c program to print prime numbers up to n and print the largest number in array.