Section 2.4 Basic Knowledge
Subgoals for evaluating an assignment statement.
-
Decide order of operations
- Decompose as necessary
-
Determine operator behavior based on operands
- Operator and operands must be compatible
-
Solve arithmetic, expression, or operation
- Decompose as necessary
Subsection 2.4.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 = 3.0
delta = 3
gamma = alpha + beta
Subsection 2.4.2 SG1: Decide order of operations
There is only one operator (an addition
+
) so that will be evaluated first.Subsection 2.4.3 SG2: Determine operator behavior based on operands
The expression
alpha + beta
is an addition between two integer values. This is a valid operation, and will produce an integer.Subsection 2.4.4 SG3: Solve arithmetic, expression, or operation
The variable
alpha
is substituted for the value 2
and the variable delta
is substituted for the value 3
. Adding them together produces the value 4
. This value is stored in gamma
.Subsection 2.4.5 Questions to check understanding
-
Is the left-hand-side (LHS) of the statement a variable? What type?
Answer.
Yes, the variablegamma
holds an integer value. -
What is the resulting type after evaluating the right-hand-side (RHS)?
Answer.
The RHS evaluates to an integer. -
Would adding
beta
instead ofdelta
instead change the resulting type?Answer.
Yes, the variablebeta
holds a float value, and adding together a float and an integer produces a float instead of an integer.
You have attempted of activities on this page.