Assignment operators are the expressions that return values. The values that they return are the values that are assigned to the variable on the left side of the assignment. The assignment operator has the right to left associativity, which means that the expressions are evaluated starting with the rightmost one.
Let's take an example:
FirstTotal = secondTotal = 0;
In the preceding example, the SecondTotal = 0 is evaluated first. This expression returns the result of the assignment, which is zero in this case. When the leftmost expression is evaluated, the rightmost expression is assigned to the leftmost variable within a parenthesis, as shown in the following code:
FirstTotal = (SecondTotal = 0);
The parentheses are not needed to force the precedence of the rightmost expression over the leftmost expression, because of the associativity of the operator.
Let's take an example:
FirstTotal = secondTotal = 0;
In the preceding example, the SecondTotal = 0 is evaluated first. This expression returns the result of the assignment, which is zero in this case. When the leftmost expression is evaluated, the rightmost expression is assigned to the leftmost variable within a parenthesis, as shown in the following code:
FirstTotal = (SecondTotal = 0);
The parentheses are not needed to force the precedence of the rightmost expression over the leftmost expression, because of the associativity of the operator.
No comments:
Post a Comment