Arithmetic Operators and Expressions
Most statements (logical lines) that you write will contain expressions . A simple example of an expression is 2 + 3 . An expression can be broken down into operators and operands.
What Is an Expression?
Most statements you write in a program will contain expressions. An expression is a combination of values and operations that produces a result. The simplest expression might be just a single number, like 5, but more commonly you will combine numbers with operators to perform calculations. When you write 2 + 3, you are creating an expression that adds two numbers together.
An expression can be broken down into two fundamental parts: operators and operands. An operand is a value (such as a number) that the operator acts upon. An operator is a symbol that tells the program what action to perform on the operands. In the expression 2 + 3, the numbers 2 and 3 are operands, and the plus sign (+) is the operator.
Anatomy of an Arithmetic Expression
To understand how expressions work, it helps to see exactly what each part does. Consider the expression 2 + 3. This expression has a clear structure: a left operand (2), an operator (+), and a right operand (3). When evaluated, this expression produces the value 5.
The same structure applies to more complex expressions. In 10 - 4, the operands are 10 and 4, and the operator is the minus sign (-). In 6 * 7, the operands are 6 and 7, and the operator is the asterisk (*). Every arithmetic expression follows this pattern: operand, operator, operand.
Common Arithmetic Operators
Programming languages support several basic arithmetic operators. Each one performs a specific mathematical operation on its operands. Understanding what each operator does is the foundation for writing correct expressions.
- Addition (+): Combines two operands by adding them together. Example: 5 + 3 equals 8.
- Subtraction (-): Finds the difference between two operands. Example: 10 - 4 equals 6.
- Multiplication (*): Multiplies two operands. Example: 6 * 7 equals 42.
- Division (/): Divides the first operand by the second. Example: 20 / 4 equals 5.
- Exponentiation (**): Raises the first operand to the power of the second. Example: 2 ** 3 equals 8.
- Integer Division (//): Divides and returns only the whole number part, discarding any remainder. Example: 20 // 3 equals 6.
- Modulus (%): Returns the remainder after division. Example: 20 % 3 equals 2.
When Multiple Operators Meet: Precedence
When an expression contains only one operator, evaluation is straightforward. But what happens when you write an expression like 2 + 3 * 4? Should the addition happen first, or the multiplication? Your knowledge of mathematics tells you that multiplication should be done first, giving you 2 + 12 = 14, not 5 * 4 = 20. This rule comes from operator precedence: the priority that determines which operators are evaluated first.
Operator precedence is the set of rules that determines the order in which operators in an expression are evaluated. Higher precedence operators are evaluated before lower precedence operators. In most programming languages, multiplication and division have higher precedence than addition and subtraction.
The precedence hierarchy for arithmetic operators, from highest to lowest, is: exponentiation (**), then multiplication (*), division (/), integer division (//), and modulus (%), and finally addition (+) and subtraction (-). This means that in any expression, exponentiation is evaluated first, then multiplication and division operations (left to right), and finally addition and subtraction (left to right).
Tracing an Expression Step by Step
Let us trace through the evaluation of 2 + 3 * 4 to see precedence in action. This expression contains two operators: addition and multiplication. According to precedence rules, multiplication has higher priority, so it is evaluated first, even though it appears to the right in the expression.
Evaluating 2 + 3 * 4
What is the value of the expression 2 + 3 * 4?
Identify operators and precedence: The expression contains two operators: + and *. Multiplication has higher precedence than addition, so we evaluate the multiplication first.
Evaluate 3 * 4: We perform the multiplication: 3 * 4 = 12. The expression now becomes 2 + 12.
Evaluate 2 + 12: Now we perform the addition: 2 + 12 = 14. This is our final result.
14
Notice that we did not evaluate the expression left to right. Even though the addition appears first in the expression, the multiplication was evaluated first because of its higher precedence. This is a crucial distinction that many beginners miss.
Associativity: When Precedence Is Tied
What happens when an expression contains two operators with the same precedence? For example, in 10 - 5 - 2, both operators are subtraction, so they have equal precedence. In this case, we use associativity to determine the order. Associativity is the rule that specifies whether operators of equal precedence are evaluated left to right or right to left.
For arithmetic operators, most are left-associative, meaning they are evaluated from left to right. Addition, subtraction, multiplication, division, integer division, and modulus are all left-associative. Exponentiation is right-associative, meaning it is evaluated from right to left.
Left-Associativity in 10 - 5 - 2
What is the value of 10 - 5 - 2?
Identify operators and associativity: Both operators are subtraction, so they have equal precedence. Subtraction is left-associative, so we evaluate from left to right.
Evaluate 10 - 5: We perform the leftmost subtraction first: 10 - 5 = 5. The expression now becomes 5 - 2.
Evaluate 5 - 2: We perform the remaining subtraction: 5 - 2 = 3. This is our final result.
3
If subtraction were right-associative, we would evaluate 5 - 2 first to get 3, then 10 - 3 to get 7. But because subtraction is left-associative, we get 3. This distinction matters, especially with operations like division and exponentiation.
Precedence and Associativity in Complex Expressions
Now let us apply both precedence and associativity rules to a more complex expression. Consider 2 + 3 * 4 - 5 / 2. This expression contains four operators: addition, multiplication, subtraction, and division. To evaluate it correctly, we must apply precedence first, then associativity.
Evaluating 2 + 3 * 4 - 5 / 2
What is the value of 2 + 3 * 4 - 5 / 2?
Identify all operators: The expression contains: + (addition), * (multiplication), - (subtraction), and / (division). Multiplication and division have higher precedence than addition and subtraction.
Evaluate high-precedence operators: First, we evaluate multiplication and division from left to right (they have equal precedence and are left-associative). 3 * 4 = 12 and 5 / 2 = 2.5. The expression now becomes 2 + 12 - 2.5.
Evaluate low-precedence operators: Now we evaluate addition and subtraction from left to right. 2 + 12 = 14, then 14 - 2.5 = 11.5. This is our final result.
11.5
Using Parentheses to Override Precedence
Sometimes you want to evaluate an expression in a different order than the default precedence rules dictate. You can use parentheses to override precedence and force certain operations to be evaluated first. Operations inside parentheses are always evaluated before operations outside them.
For example, if you want to add 2 and 3 first, then multiply by 4, you would write (2 + 3) * 4. Without the parentheses, 2 + 3 * 4 would multiply 3 and 4 first, giving 14. With the parentheses, you get 5 * 4 = 20. Parentheses make your intention clear and can prevent bugs caused by unexpected evaluation order.
Common Mistakes with Operator Precedence
Evaluating expressions left to right without considering precedence
Multiplication has higher precedence than addition, so 3 * 4 must be evaluated first, giving 2 + 12 = 14.
Fix:
Always check the precedence of operators before evaluating. Multiplication and division come before addition and subtraction.Forgetting that exponentiation has the highest precedence
Exponentiation has higher precedence than addition, so 3 ** 2 must be evaluated first, giving 2 + 9 = 11.
Fix:
Remember that exponentiation is evaluated before all other arithmetic operators.Assuming all operators of equal precedence are evaluated right to left
Subtraction is left-associative, so you must evaluate from left to right: 10 - 5 = 5, then 5 - 2 = 3.
Fix:
For arithmetic operators, remember that most are left-associative. Only exponentiation is right-associative.Forgetting that division and multiplication have equal precedence
Division and multiplication have equal precedence and are left-associative, so you evaluate left to right: 20 / 4 = 5, then 5 * 2 = 10.
Fix:
When operators have equal precedence, evaluate from left to right.
Practice: Evaluating Expressions
Evaluate each of the following expressions using the precedence and associativity rules you have learned. Write down the order in which you evaluate the operators, then write the final result.
Hints
- Remember that exponentiation has the highest precedence, followed by multiplication and division, then addition and subtraction.
- When operators have equal precedence, evaluate from left to right (except for exponentiation, which is right-associative).
- Use parentheses to check your work: rewrite the expression with parentheses showing the order of evaluation.
- 5 + 2 * 3
- 10 - 4 - 2
- 2 ** 3 * 2
- 20 / 4 + 3
- 6 + 8 / 2 - 1
- 2 ** 3 ** 2
Summary
- An expression is a combination of operands (values) and operators (actions) that produces a result. Every expression can be broken down into these two fundamental parts.
- Operator precedence determines the order in which operators are evaluated. Exponentiation has the highest precedence, followed by multiplication and division, then addition and subtraction.
- When operators have equal precedence, associativity determines the order. Most arithmetic operators are left-associative (evaluated left to right), but exponentiation is right-associative (evaluated right to left).
- Parentheses override precedence and force certain operations to be evaluated first. Using parentheses makes expressions clearer and helps prevent bugs.
- Understanding precedence and associativity is essential for writing correct expressions and predicting their results accurately.
Key Takeaways
- An expression combines operands (values) and operators (actions) to produce a result; the simplest example is 2 + 3.
- Operator precedence determines evaluation order: exponentiation is highest, followed by multiplication and division, then addition and subtraction.
- Associativity resolves ties between equal-precedence operators; most arithmetic operators are left-associative (evaluated left to right).
- Parentheses override precedence and make expressions clearer; use them liberally to ensure your intent is unmistakable.
- Tracing expressions step by step using precedence and associativity rules prevents common mistakes and builds confidence in predicting results.