Operators and Their Precedence
An expression is a combination of values, variables, and operators, or a single value or variable by itself.
Expressions Begin with Small Pieces
An expression is a combination of values, variables, and operators, or it can be a single value or variable by itself. This means expressions are not limited to long calculations. The value 42 is an expression, and the variable name score is also an expression. These simple expressions are the building blocks for larger computations.
In the expression total * 2, total and 2 are operands: they are the pieces the operation uses. The asterisk is the operator: it tells the program what operation to perform. A compound expression therefore combines operands and an operator into something that produces a result.
From Variable to Result
When a variable appears in an expression, the program substitutes the variable's current stored value before evaluating the expression. For example, if a is 8 and b is 3, the expression a + b is treated as 8 + 3 during evaluation, producing 11.
Evaluating a + b
If a is 8 and b is 3, evaluate the expression a + b.
Identify the pieces: a and b are variables, and + is the operator.
Substitute current values: Replace a with 8 and b with 3, giving 8 + 3.
Apply the operator: Apply the addition operation to the substituted values.
The expression evaluates to 11.
Precedence in Compound Expressions
Precedence is the question of which operation is applied first when a compound expression contains more than one operator. The source material establishes the essential evaluation process: identify the operands, substitute current variable values, apply the operators, and obtain a result. It does not provide a language-specific precedence table or state which of several different operators must be applied first.
Results Do Not Rewrite Variables
Evaluating an expression produces a result, but it does not change the original variable unless you explicitly assign the result back. If score appears in score + 10, the expression uses the current value of score and produces a new result. The expression itself does not update score.
Using a Variable Without Changing It
Suppose score stores 25. Consider the expression score + 10.
Read the variable: The current value stored in score is 25.
Substitute the value: The expression becomes 25 + 10.
Evaluate: The expression produces a new result by applying the plus operator.
Check the original variable: Evaluating the expression alone does not change the value stored in score.
The expression produces 35, while score remains unchanged unless the result is explicitly assigned back.
Mistakes During Evaluation
Thinking only multi-part calculations are expressions
A single value or a single variable name qualifies as an expression.
Fix:
Recognize bare values and variable names as the simplest expressions.Skipping variable substitution
The program substitutes each variable's current value before evaluating the expression.
Fix:
Write the substituted expression first, then apply its operator.Treating an expression's result as an automatic variable update
An expression produces a result but does not change the original variable by itself.
Fix:
Distinguish evaluating an expression from explicitly assigning its result back.Guessing a precedence order without checking the language rules
The supplied material does not provide a precedence table, so a specific ordering cannot be concluded from it.
Fix:
Use the relevant programming language's documented operator rules.
Practice the Evaluation Process
Suppose quantity stores 6. Identify the operands and operator in quantity * 2, substitute the variable's current value, and state the result produced by the expression. Then explain whether evaluating the expression alone changes quantity.
Hints
- The variable is one operand, and 2 is the other operand.
- Replace quantity with its current value before applying the operator.
- Separate the produced result from any explicit assignment back to quantity.
Practice Solution
Evaluate quantity * 2 when quantity stores 6.
Identify the parts: quantity and 2 are operands, and * is the operator.
Substitute: Replace quantity with 6, giving 6 * 2.
Apply the operator: Apply the multiplication operation to the substituted values.
Check the variable: The expression produces a result but does not change quantity by itself.
The expression produces 12, and quantity remains unchanged unless the result is explicitly assigned back.
Key Takeaways
- An expression can be a combination of values, variables, and operators, or a single value or variable.
- Variables in expressions are replaced by their current stored values before evaluation.
- Operators act on operands and produce a result.
- Evaluating an expression does not change an original variable unless the result is explicitly assigned back.
- Precedence concerns the order of operations in a multi-operator expression; the exact rules must come from the programming language being used.
Key Takeaways
- Expressions range from a single value or variable to a compound combination of operands and operators.
- Evaluation substitutes each variable with its current stored value before applying operators.
- A compound expression produces a result, but the original variables remain unchanged unless explicitly updated.
- Operator precedence determines operation order in expressions with multiple operators, but specific precedence rankings depend on language rules not supplied here.