Subgoals

Section2.10Worked Example: Operation Precedence

Subgoals for evaluating an assignment statement.

1. Determine resultant data type of expression
2. Update variable for pre operators based on side effect
3. Solve arithmetic equation, operator precedence
4. 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.
5. Update variable for post operators based on side effect

Subsection2.10.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 = 1;
int delta = 3;
int eta, gamma;
double omega = 2.5;
double theta = -1.3;
double kappa = 3.0;
double lambda, rho;

gamma = delta / (alpha + beta) % alpha;


Subsection2.10.2SG1 : Determine resultant data type of expression

The expression is the right-hand-side (RHS) of the statement: delta / (alpha + beta) % alpha. At the beginning of the snippet, all of these variables were declared as int type, so all of the operations will also result in int values.
If the expression had included any double values, the operations dealing with any double values would promote any int value on the other side of the operator to a double value, and the result would be a double, but that is not the case here.

Subsection2.10.3SG2: Update variable for pre-increment or pre-decrement operators (side effect)

NOT USED IN THIS EXAMPLE

Subsection2.10.4SG3: Evaluate arithmetic expression according to operator precedence

• The RHS may be easiest to conceptualize algebraically, by replacing the variables right away with their initialized values from the declarations above: 3 / (2 + 1) % 2.
• In the order of operations, parentheses have highest precedence, so we evaluate the addition within the parentheses first, resulting in a new expression: 3 / 3 % 2.
• Division and modulus have the same precedence, so we evaluate them left to right: 3 / 3 is 1, then 1 % 2 is 1. (Remember, the mod % operation returns the remainder after integer division.)

Subsection2.10.5SG4: 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 is a variable of type int, and the RHS is type int. This is valid.

Subsection2.10.6SG5: Update variable for post-increment or post-decrement operators (side effect)

NOT USED IN THIS EXAMPLE

Subsection2.10.7Questions to check understanding

int alpha = 2;
int beta = 1;
int delta = 3;
int eta, gamma;
double omega = 2.5;
double theta = -1.3;
double kappa = 3.0;
double lambda, rho;

gamma = delta / (alpha + beta) % alpha;

Q1) In line 5 above, is the LHS of the statement a variable? What data type?
Q2) What is the resulting data type after evaluating the RHS of line 5?
Q3) Can the data type of the RHS result be assigned to the LHS variable in line 5?