Section 2.10 Operation Precedence
Subgoals for evaluating an assignment statement.
Subsection 2.10.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?
alpha = 2
beta = 1
delta = 3
omega = 2.5
theta = -1.3
kappa = 3.0
gamma = delta / (alpha + beta) % alpha
Subsection 2.10.2 SG1: Decide order of operations
The expression is the right-hand-side (RHS) of the statement:
delta /(alpha + beta) % alpha
. In this expression, the parentheses have the highest priority, so the addition inside of the parentheses will be evaluated first. Then, since division and modulo have the same priority, the division with delta
will occur next, with the result module the final alpha
Subsection 2.10.3 SG2: Determine operator behavior based on operands
The expression
alpha + beta
is an addition between two integers, so will result in an integer. The subsequent division between delta
(an integer) and that new integer will result in a float, since the float division operator is used. The final modulo will be between a float and an integer, which will produce a float.
Subsection 2.10.4 SG3: Solve arithmetic, expression, or operation
The variable
alpha
is substituted for the value 2
and the variable beta
is substituted for the value 1
. Adding them produces the integer 3
.
Then, the last
alpha
is substituted for 2
, and 1.0 % 2
is calculated, with the final remainder of 1.0
.
Subsection 2.10.5 Questions to check understanding
-
Is the left-hand-side (LHS) of the statement a variable? What type?
-
What is the resulting type after evaluating the right-hand-side (RHS)?
-
Answer.
You have attempted 1 of 1 activities on this page.