Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Evaluation order

Mani Pava
Greenhorn
Posts: 5
Please try this piece of code in JAVA and C++, and you will get two different answers...

java code {

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

}

cpp code {

int j = 0 ;
int k = 5 ;
j = k++ + --k ;
cout << j ;
cout << k ;

}

Answers:
JAVA : j=10, k=5
CPP : j=8, k=5

Please explain....

Thanks in Advance,
Mani

Rich Wright
Greenhorn
Posts: 17
I ran both and got what you got for Java but got 9 and 6 for C & C++. I used gcc and g++. The order of precedence is different for the two languages.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
The reason why the results are different is simply that C++ and Java are different programming languages, with different rules for the evaluation of expressions.

If you want to know exactly why you get the answer that you do in Java, you can study the Java Language Specification.

Rich: Are you sure you got 9 and 6, and that you are running the exact code shown above? Because I just tried it out (with g++ 4.2.3 and Java 6 update 6 on Ubuntu Linux) and I do get 8 and 5 for C++, and 10 and 5 for Java.
[ April 29, 2008: Message edited by: Jesper Young ]

Nickolas Case
Ranch Hand
Posts: 98
Java does the arithmetic first then increments/decrements. Hence:
J = 5 + 5
K(5)++ = 6
--K(6) = 5

so J = 10 and K = 5

I am not a C guru but it looks like C does subtraction and then addition. arithmetic before increment/decrement. So..
Since there is no arithmetic subtraction...
J = 5++ + 4
Now we do the arithmetic:
J = 9
Now we increment our 5

The end result:
J = 9
K = 6

Hope this helps and if I am wrong on the C someone please chime in set the record straight.

Rob Spoor
Sheriff
Posts: 20527
54
I think I read somewhere that the C and C++ specifications don't specify what the executing order should be, therefore leaving it completely to the compiler developer. Basically, you could get different results with the same code but 2 different compilers.
[ April 29, 2008: Message edited by: Rob Prime ]

Campbell Ritchie
Sheriff
Posts: 48917
58
Originally posted by Rob Prime:
I think I read somewhere that the C and C++ specifications don't specify what the executing order should be, therefore leaving it completely to the compiler developer. Basically, you could get different results with the same code but 2 different compilers.
You do. A sinilar question came up about 2 years ago and I tried it with three C compilers. I think it was

int i = 4;
i = i++;
printf("%d", i);

in C. Code from two of my compilers printed 4 and the 3rd printed 5!

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
Yep, Rob is correct. There are many computations in C/C++ the result of which is officially undefined, meaning it can be anything convenient for the compiler-writer. One of Java's strengths is that its specification eliminates virtually all of these undefined cases; the remaining ones have to to with multithreading.

Rich Wright
Greenhorn
Posts: 17
I checked and got the same results. It was on a sparc workstation with Solaris 10 using g++ 3.3.2 and and I also tried 3.4.6.

I guess this is a good reason to use () to be sure you're going to get what you what.

Campbell Ritchie
Sheriff
Posts: 48917
58
Originally posted by Nickolas Case:
Java does the arithmetic first then increments/decrements.
No, the post-increment operator has the highest precendence in the Java precendence tables. The confusion arises because it returns the OLD VALUE.

Pre-increment has a lower precedence than post-increment, and higher than arithmetic, and returns the NEW VALUE.

If you find this confusing, then keep all your increment and decrement operators in statements by themselves.

 Don't get me started about those stupid light bulbs.