Skip to main content

Section 2.17 Worked Example: Prefix 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.17.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;

eta = beta + delta % ++alpha;

Subsection 2.17.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.17.3 SG2: Update variables for any pre-increment or pre-decrement operators (side effects)

The ++ and -- syntax indicates pre- or post- side effect assignments upon variables within an expression. The expression a + 1 does not alter the value of the variable a, however a++ adds 1 to the value of 1 resulting in a new value of a.
An increment is an increase of 1, while a decrement is a decrease of 1. Generally, we only want to perform these operations on int values, but it is valid to perform them on double values.
It is important to note that the statement ++delta; would be a valid line of Java code all on its own, adding 1 to the value of delta and assigning back into the delta variable.
In this example, ++alpha is a pre-increment operation, so we increment alpha, and the new assigned value of alpha is 3 (it was initialized to 2).

Subsection 2.17.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 : 1 + 3 % 3. (Remember, alpha had a pre-increment, so it is already 1 more than it was initialized.)
  • In the order of operations, modulus has higher precedence than addition, so we evaluate the mod first: 3 % 3 is 0. (Remember, the mod % operation returns the remainder after integer division.)
  • Finally, we evaluate the addition: 1 + 0 is 1.

Subsection 2.17.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 int, and the RHS is type int. This is valid.

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

It is wise to double-check whether the ++ or -- were on the left or right of the variables, to be sure whether we perform a pre or post side effect assignment. In this case, the only increment was a pre-increment, so we do not need to peform any post side effect assignments.

Subsection 2.17.7 Questions to check and extend understanding

Q1) What are the final values of alpha, beta, delta and eta?
Q2) The following is run: eta = beta + delta % --alpha;. What is the resulting value of eta?
Answer.
Q1-Answer) alpha is 3; beta is 1; delta is 3; eta is 1;
Q2-Answer) 2

Practice Pages.

You have attempted of activities on this page.