I am a little unsure of my understanding of what is going on here and why.
k = 1;
k += k = 4 * k + 2;
output is 7.
is this what is happening?
k += k = (4 * k) + 2;
I understand the basic concept that * happens before +. Does it also work before the = assignment? I know this is a very basic concept but when I see more than one assignment operator it confuses me a little still.
k += k = (4 * 1) + 2; // because * has greater precedence than +
k += k = 4 + 2; // expressions are evaluated from right to left
k += k = 6; // left-most k still has the original value as 1
k += 6; // 1 + 6 is 7
k = 7;
It take a while to get your head wrapped around these expressions
The trick is that even though the results of operations depend on order of precedence, variables are always evaluated from left to right. It should be more clear when we write the += operator out completely:
Could you please elaborate more why after k += k = 6; the left-most k still has the original value as 1 ? I thought that after assignment k = 6 it would change the value to 6 so it will be 6+6 not 1+6.
Stephan van Hulst
posted 8 years ago
Because variables are evaluated before calculations are done with them, and they are evaluated from left to right, as they are needed.
The += and = operators have the same precedence but they are right-associative, meaning they are performed from right to left. However, the variables are still evaluated from left to right, and this is done before any calculations.
k += k = 6 is shorthand for k = (int) k + (k = 6). Since the k = 6 has a higher precedence than the addition, 6 gets assigned to k before it is added to k. However, the *value* used in the addition on the left side, is already evaluated before the assignment is done, so what actually happens is this:
Javin Paul wrote:
Always remember while evaluating expression two things comes in picture
1) Precedence of operation ( higher will be executed first)
2) Associativity (either right to left or left to right)
Also the approach shown here is quite good (solving expression by breaking them and starting it from right most)
There is actually three "things" that needs to "comes in picture". The operator precedence is used to determine grouping, as if parens were added. It is actually not used to determine what will be "executed first". The assoc clarifies the precedence, for operators with the same precedence.
And finally, there is the order of evaluation. This is defined in the specification, and it is independent of the operator precedence. Normally, you don't need to know about the evaluation order; and understanding the operator precedence is good enough. When it needs to be understood, is when there are side-effects to the expression, when the actual evaluation order is important... expressions with assignments whose value is used, post and prefix operators, etc., fall within this category.
Great topic, but it's a little bit over the top for the real exam.
Older versions of the exam put emphasis on this kind of intricate precedence discussion, but the 5 and 6 exams don't. You might encounter one question that covers a little bit of this, but for the most part on the newer exams, the exam team decided not to focus on that (you'll see lots of parenthesis to make the order of operation clear.)
So, my recommendation is this:
- treat this as a a low priority topic
- if you have all the more important topics completely, absolutely under control, then you could choose to study this.
- in the real world, use parenthesis
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Stephan van Hulst
posted 8 years ago
Bert Bates wrote:- in the real world, use parenthesis
I'd like to add:
- in the real world, don't embed compound assignment operators in larger expressions.
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
create, convert, edit or print DOC and DOCX in Java