Statements vs. Expressions
An expression is a combination of values, variables, and operators, or a single value or variable by itself.
A Value Is Already an Expression
When learners first hear the word expression, they often imagine a long calculation containing several operators. An expression can be much simpler than that. A bare value such as 42 is an expression, and a variable name such as score is also an expression. Both can be evaluated to produce a result.
An expression is a combination of values, variables, and operators, or a single value or variable by itself.
Parts of a Compound Expression
A compound expression combines values, variables, and operators. The values or variables being acted on are the operands. The operator tells the program what operation to perform on those operands. In score + 10, score is one operand, 10 is the other operand, and + is the operator.
Labeling total multiplied by 2
Identify the pieces of the expression total * 2.
Variable operand: total is one operand because it is a variable used by the expression.
Operator: * is the multiplication operator. It specifies the operation performed with the operands.
Value operand: 2 is the other operand because it is a literal value used by the expression.
Evaluation: The program substitutes the current value of total, performs the multiplication, and returns the result.
The expression contains a variable operand, a multiplication operator, and a value operand.
Substitution Before Evaluation
A variable inside an expression stands for its current stored value. Before the operation is completed, the program substitutes that current value for the variable. This is why the same expression can produce different results when the stored values of its variables differ.
Evaluating a plus expression
Suppose a is 8 and b is 3. Evaluate the expression a + b.
Start with the expression: The expression is a + b. The variable names are the operands, and + is the operator.
Substitute current values: Replace a with 8 and b with 3, producing 8 + 3.
Apply the operator: Apply the plus operator to the substituted values.
The expression evaluates to 11.
Results and Variable Changes
Evaluating an expression produces a result, but that evaluation does not change the original variable by itself. For example, evaluating score + 10 creates a new result based on the current value of score. The original variable remains unchanged unless the result is explicitly assigned back to it.
| Situation | What happens to the expression | What happens to the original variable |
|---|---|---|
| Evaluate an expression | A result is produced | It does not change automatically |
| Assign the result back | The result is used as the assigned value | The variable changes because the result is explicitly assigned |
Common Recognition Mistakes
Assuming an expression must contain an operator.
A single value or a single variable name is also an expression.
Fix:
Check first whether the item is a value or variable. It can qualify as an expression even without an operator.Treating a variable name as if it were the result of an expression.
The program substitutes each variable with its current stored value before applying the operator.
Fix:
Write the substituted values first, then apply the operator.Assuming that evaluating an expression changes its variables.
An expression produces a result but does not change the original variable unless the result is explicitly assigned back.
Fix:
Separate the result of evaluation from any later explicit assignment.Ignoring the roles of the expression's parts.
The variable and value are operands, while the operator specifies the operation.
Fix:
Label each operand and the operator before tracing the evaluation.
When an expression seems confusing, trace it in three steps: identify its operands, substitute the current stored values of any variables, and apply the operator to produce the result.
Practice the Evaluation Trace
Suppose points is 12 and bonus is 5. Trace the expression points + bonus. Name the operands and operator, show the expression after substituting the current values, and state the result. Then explain whether evaluating the expression alone changes points.
Hints
- The operands are the variable names before substitution.
- Replace points with 12 and bonus with 5 before applying +.
- Use the distinction between producing a result and explicitly assigning it back.
What do you think happens?
If a is 8 and b is 3, what result does a + b produce after substitution?
Reveal answer
Answer: 11
The program substitutes a with 8 and b with 3, producing 8 + 3, and then applies the plus operator. Evaluating the expression produces 11 without automatically changing the original variables.
Key Takeaways
- An expression can be a combination of values, variables, and operators, or a single value or variable by itself.
- Values and variables used by an operator are operands; the operator specifies what operation to perform.
- When a variable appears in an expression, its current stored value is substituted before evaluation.
- Evaluating an expression produces a result, but it does not change the original variable unless the result is explicitly assigned back.
- A reliable evaluation trace identifies the parts, substitutes current values, and then applies the operator.