Skip to main content

Section 2.4 Basic Knowledge

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 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

  1. Is the left-hand-side (LHS) of the statement a variable? What type?
    Answer.
    Yes, the variable gamma holds an integer value.
  2. What is the resulting type after evaluating the right-hand-side (RHS)?
    Answer.
    The RHS evaluates to an integer.
  3. Would adding beta instead of delta instead change the resulting type?
    Answer.
    Yes, the variable beta 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.