1. 1st digit printed is 1
2. 1st digit printed is 2
3. 1st digit printed is 3
4. 2nd digit printed is 1
5. 2nd digit printed is 2
6. 2nd digit printed is 3
Why don't the precedence rules affect the answer?
That is, shouldn't what's in the Parentheses be resolved first, followed by resolving the Less Than, <, and lastly resolving the Or, ||?
It seems to me the jls has turned the set of precedence rules on its head. That is, the jls seems to say "here are the precedence rules, where || has less priority than parentheses and less priority than < except sometimes those precedences don't matter and || overrides everything else with the highest precedence". Is this a nuance or am I missing a rule?
What wasn't said in that old thread (not explicitly, anyway) was that evaluation of expressions proceeds from left to right. In other words the LHS of an expression is always evaluated before the RHS -- and as you know, the RHS may not be evaluated at all.
Your first example, A + B * C, is perfect. The * operator is evaluated before the + operator, but after the variable A is evaluated.
What of A || B * C?
Noted, a more realistic expression would be (A || (0 < (B * C))).
Like your example, the * trumps the ||.
And then, if we were to substitute (0 < (B * C)) with (0 < (someInt++)), we’re back to the beginning in that from my way of looking at it (contrary as it is to your way and the compiler’s way) is that someInt++ is executed before short-circuiting occurs.
I appreciate the time you’ve put into this subject and cordially invite you to take a pass rather than extending the…
Let's take the expression A || 0 < B * C. Arithmetic has a higher precedence than relational operators, which in turn have higher precedence than logical operators.
However, the expression is always evaluated from left to right. In this example, if A was true, the expression would evaluate to: true || 0 < B * C, which in turn evaluates to true, because of short-circuit evaluation.
If A is false, the result would be: false || 0 < B * C, which evaluates:
So as you can see, if we replace B and C with some sort of increment expression, it only gets evaluated if A is false.
Stephan nad Paul, I would appreciate if you have a look at the code below and answer my query. What if we write a code like this-
I know that expression (true || (0 >= ++i)) will be solved left to right and true will be assigned to t without execution of (0 >= ++i)). But the expression (true || (0 >= ++i)) is enclosed into parenthesis and priority of parenthesis is higher than assignment. So, shouldn't the whole expression inside parenthesis be executed first before assignment. why this is not executed like this-
Please help me to be clear about this. Thanks in advance.