8.2 - Operators
What operators are and why they matter
Operators are special symbols or keywords in programming that perform operations on values or variables. They are essential for tasks like calculations, comparisons, and assigning data. Understanding operators is key to writing functional programs, as they form the building blocks of expressions and logic.
Arithmetic operators for calculations
Arithmetic operators perform mathematical operations on numbers, taking two values (operands) and producing a result. They are used to add, subtract, multiply, divide, and more. These operators can handle integers, reals, or a mix, but be cautious with division in some languages, as dividing integers might truncate the result (e.g., 7 / 3 could give 2 instead of 2.33).
Key arithmetic operators and examples
| Function | Typical Operator | Example | Result | Notes |
|---|---|---|---|---|
| Addition | + | 7 + 2 | 9 | Adds two numbers |
| Subtraction | - | 5 - 12 | -7 | Subtracts the second from the first |
| Multiplication | * | 6 * 7 | 42 | Multiplies two numbers |
| Division | / | 36 / 4 | 9 | Divides the first by the second; may truncate for integers |
| Exponentiation | ** (in Python) | 3 ** 2 | 9 | Raises the first number to the power of the second |
| Quotient (integer division) | // (in Python) | 17 // 5 | 3 | Gives the whole number part of division |
| Remainder (modulo) | % | 17 % 5 | 2 | Gives the remainder after division |
In Python, use // for quotient (like DIV) and % for remainder (like MOD) to avoid issues with regular division truncating integers. For example, 7 / 3 in some languages gives 2, but 7 // 3 reliably gives 2, and 7 % 3 gives 1.
Order of operations and brackets
Programming languages often follow BODMAS rules: Brackets first, then Orders (exponents), Division and Multiplication (from left to right), and finally Addition and Subtraction (from left to right). This means expressions are evaluated in a specific order, which can affect results.
Examples of order of operations:
- Without brackets:
3 + 7 * 4evaluates to 31 (multiplication first: 7 * 4 = 28, then +3). - With brackets:
(3 + 7) * 4evaluates to 40 (addition first: 3 + 7 = 10, then *4).
Always use brackets () to force the desired order and make your code clearer. Here's a short Python example:
This ensures your calculations match what you intend, preventing unexpected outcomes.
Assignment operators for storing values
The assignment operator assigns a value to a variable or constant, storing data for later use. In most languages, it's represented by =. The left side must be a variable name, and the right side is the value or expression to assign.
How assignment works
Assignment follows these principles:
- Basic assignment - Sets a variable to a specific value.
- Updating values - Can use the variable itself in the expression, like incrementing it.
For example, in Python:
The right side is evaluated first, then stored in the left-side variable. This is useful for counters or accumulating totals in loops.
Comparison operators for decision-making
Comparison operators compare two values and return a Boolean result: True or False. They are crucial for conditions in if-statements, loops, and decisions. Unlike assignment, they don't change values—they just check relationships.
Key comparison operators
| Operator | Meaning | True Example | False Example |
|---|---|---|---|
| Equal to | 8 8 | 8 12 | |
| != | Not equal to | 10 != 11 | 10 != 10 |
| < | Less than | 7 < 15 | 6 < 4 |
| > | Greater than | 18 > 10 | 12 > 15 |
| <= | Less than or equal to | 9 <= 10 | 13 <= 12 |
| >= | Greater than or equal to | 5 >= 5 | 11 >= 14 |
These produce Booleans, which are data types representing true or false states. For example, in Python:
Use them in conditions, like if age </mark> 30: to check equality.
Common mistakes with operators
Mixing up operators is a frequent error that can cause code to fail or behave unexpectedly. Pay close attention to these pitfalls to debug effectively.
Mixing assignment and comparison
Key differences:
- Assignment
=stores a value (e.g.,age = 30sets age to 30). - Comparison
<mark>checks equality (e.g.,if age </mark> 30:only runs if age is already 30). - Mistake example: Using
if age = 30:would assign 30 to age and treat the condition as always true, which is not intended.
In Python, this might cause a syntax error or unintended logic. Always double-check: use = for assigning, <mark> for comparing.
Other pitfalls
Common programming errors include:
- Forgetting brackets in arithmetic expressions, leading to wrong order of operations.
- Assuming division always gives decimals—use quotient and remainder for integer results.
- Not considering data types: Comparing a string "5" with integer 5 using
</mark>might give False in some languages.
Test your code with simple examples to spot these issues early. For instance, trace through 17 % 5 to confirm it gives 2, not a decimal.