Section 2.14 Worked Example: Compound Operators
Subgoals for evaluating an assignment statement.
- Determine resultant data type of expression
- Update variable for pre operators based on side effect
- Solve arithmetic equation, operator precedence
- Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
- Update variable for post operators based on side effect
Subsection 2.14.1
You can watch this video or read through the content below it.
Given the following code snippet, evaluate the final statement (the last line). If invalid, give the reason. If valid, what value is assigned to the variable? Note any possible side effects.
int alpha = 2;
int beta = 5;
int delta = 7;
int eta;
int gamma = 5;
double omega = 2.5;
double theta = -1.3;
double kappa = 3.0;
double lambda;
double rho;
gamma += delta / alpha + beta % alpha;
Subsection 2.14.2 SG1 : Determine resultant data type of expression
- The expression is usually the right-hand-side (RHS) of the statement, but compound assignment operators are a special shorthand than includes the compounded operation with the left-hand-side (LHS). A simple example is
gamma += 1;
, which is shorthand forgamma = gamma + 1;
- In the code given above, the RHS expression is
delta / alpha + beta % alpha
. This means that the valuegamma + delta / alpha + beta % alpha
will be assigned togamma
. - At the beginning of the snippet, all of these variables were declared as
int
type, so all of the operations will also result inint
values.
Subsection 2.14.3 SG2: Update variable for pre-increment or pre-decrement operators (side effect)
NOT USED IN THIS EXAMPLE
Subsection 2.14.4 SG3: Evaluate arithmetic expression according to operator precedence
- The expression may be easiest to conceptualize algebraically, by replacing the variables right away with their initialized values from the declarations above:
5 + 7 / 2 + 5 % 2
. - Division and modulus both have higher precedence than addition, so we evaluate them first, resulting in a new expression of
5 + 3 + 1
. - In the division operation, both variables are of type
int
, so the result is also anint
value. Any fractional piece of the quotient is truncated (thrown away). As an example,5 / 3
yields the value1
. - Also remember, the mod % operation returns the remainder after integer division.
- Finally, we evaluate the additions left-to-right, 5+3 is 8, and 8+1 is 9.
Subsection 2.14.5 SG4: Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
The LHS of the assignment is a variable of type
int
, and the expression result is type int
. This is valid.Subsection 2.14.6 SG5: Update variable for post-increment or post-decrement operators (side effect)
NOT USED IN THIS EXAMPLE
Subsection 2.14.7 Questions to check understanding
Q1) Is the left-hand-side (LHS) of the statement a variable? What type?
Q2) What is the resulting type after evaluating the right-hand-side (RHS)?
Q3) Can the type of the RHS result be assigned to the LHS variable?
Answer.
Q1-Answer) Yes, gamma is declared as an int
Q2-Answer) On the RHS, all of the values are int, so the result is also int
Q3-Answer) Yes, an int can be assigned to an int
Practice Pages.
You have attempted of activities on this page.