Skip to main content

Section 2.19 Worked Example: Prefix and Postfix Operators

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

Subsection 2.19.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;
int gamma;

double omega = 2.5;
double theta = -1.3;
double kappa = 3.0;
double lambda;
double rho;

lambda = ++beta / delta-- * alpha;

Subsection 2.19.2 SG1 : Determine resultant data type of expression

The expression is the right-hand-side (RHS) of the statement: ++beta / delta-- * 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.

Subsection 2.19.3 SG2: Update variables for any pre-increment or pre-decrement operators (side effects)

In this example, we see one pre-increment with ++beta so we increment beta before we evalute the rest of the expression. The initial value of beta was 1, so a side-effect of this statement is to assign beta the new incremented value of 2.

Subsection 2.19.4 SG3: Evaluate arithmetic expression according to operator precedence

  • The RHS may be easiest to conceptualize algebraically, by replacing the variables right away with their current values : 2 / 3 * 2. (Remember, beta had a pre-increment so its value has already been increased by 1. Note that the access to the variable delta is its original value.)
  • In the order of operations, division and multiplication have the same precedence, so we evaluate them left to right. First, 2/3 is 0 (Remember, integer division truncates the quotient by discarding any fractional value). Thus the arithmetic expression is 0 * 2 which is a value of 0.

Subsection 2.19.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 is a variable of type double, and the RHS is type int. This is valid, as the result of the expression will be promoted to a double.

Subsection 2.19.6 SG5: Update variable for post-increment or post-decrement operators (side effect)

In this example we see one post-decrement with delta—so we decrement delta after we evaluate the rest of the expression. The initial value of delta was 3, so a side-effect of this statement is to assign the value 2 to delta.

Subsection 2.19.7 Questions to check and extend understanding

Q1) What are the final values of alpha, beta, delta and lambda?
Q2)
int x = 5;
int y = x--;
int z = ++x;
Do y and z have the same value? Why or why not?
Answer.
Q1-Answer) alpha is 2; beta is 2; delta is 2; lambda is 0.0;
Q2-Answer) They are the same. y is initialized before the -- operator is applied to x, so it is set to 5 before x is set to 4. z is initialized after the ++ operator is applied, so it is set to x after x is set to 5.

Practice Pages.

You have attempted of activities on this page.