This is my first post here so please forgive any protocol errors.
My question is simply trying to understand what is happening with the following Java code. I fully understand that the use of parentheses would clarify everything, but the resulting output seems to fly in the face of convention regarding Java order of operations.
public class Tester
{
public static void main(String[] args)
{
int total=9, num=13;
if (total>4 || ++num>15 && total>0)
{
System.out.println("short");
}
System.out.println(num);
}
}
The output is: short 13
It is obvious the ++num did not execute. If strict order of operations had been observed it should have been the first thing to happen. It didn't. So next is the &&. If the && is done by order of precedence over the ||, then the same...the ++num should happen first. It didn't. So, to me it seems the output was determined with the || executed first, shortciruiting the ++num and then, working with the &&, resulted in the short being printed. Were the order of operation rules simply ignored and the Boolean expression executed left to right? Is the increment operator causing the irregular behavior?
Thanks for any insight as to what is actually going on with this code.
var = var + 1one line above of where it appears in most of the cases. The same code in C++ yields 14. Could someone comment on this difference between C++ and Java?subq $16, %rsp movl $9, -4(%rbp) movl $13, -8(%rbp) cmpl $4, -4(%rbp) jg .L2 *addl $1, -8(%rbp)* cmpl $15, -8(%rbp) jle .L3 cmpl $0, -4(%rbp) jle .L3For the if. Id est. ++ only happens after the ||. Perhaps I mistook the output of my java program using |. Too much coffee :)