Skip to main content

Section 1.7 Basic Operation Precedence

Subgoals for evaluating an assignment statement.

  1. Decide order of operations
    1. Decompose as necessary
  2. Determine operator behavior based on operands
    1. Operator and operands must be compatible
  3. Solve arithmetic, expression, or operation
    1. Decompose as necessary

Subsection 1.7.1

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.
alpha = 3
beta = 1
delta = 3
omega = 2.5
theta = -1.3
kappa = 2.0

mu = alpha / kappa + delta

Subsection 1.7.2 SG1: Decide order of operations

The expression is the right-hand-side (RHS) of the statement: alpha / kappa + delta. In this expression, division has a higher precedence than summation, so we first evaluate alpha / kappa. Then, we will evaluate the addition of delta to that result.

Subsection 1.7.3 SG2: Determine operator behavior based on operands

The expression alpha / kappa is a division between an integer (alpha) and a float (kappa). That is a valid operation, and will produce a float. That float will be added to delta, which is an integer. Adding together a float and an integer is also valid, and will produce a float.

Subsection 1.7.4 SG3: Solve arithmetic, expression, or operation

The variable alpha is substituted for the value 3 and the variable kappa is substituted for the value 2.0. Dividing them produces the float 1.5. The value of delta (3) is then added to the result, for a total of 4.5. This value is stored in mu.

Subsection 1.7.5 Questions to check understanding

  1. Is the left-hand-side (LHS) of the statement a variable? What type?
    Answer.
    Yes, the variable mu will be a float.
  2. What is the resulting type after evaluating the right-hand-side (RHS)?
    Answer.
    A float.
  3. Would dividing kappa by alpha instead change the resulting type?
    Answer.
    Although it would change its value, it would not change its type. Division with / always produces a float.
You have attempted of activities on this page.