Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Arithmetic operators are the foundation of every calculation your programs will ever perform. Whether you're building a game that tracks scores, a financial app that calculates interest, or an algorithm that processes data, you're relying on these operators to manipulate numbers. In Programming Languages I, you're being tested on more than just knowing that + adds things—you need to understand operator behavior, type interactions, and evaluation rules that determine what your code actually produces.
The key concepts here include operator precedence, integer vs. floating-point division, commutativity, and side effects in expressions. When you encounter exam questions about unexpected output or debugging scenarios, the answer often traces back to how these operators behave with different data types. Don't just memorize symbols—know what each operator returns, when it might surprise you, and how to use it strategically.
These operators take two operands and return a result without modifying the original values. They form the core of mathematical computation in programming and follow predictable rules based on mathematical properties.
-x negates a single value, serving a different purpose than binary subtraction"hi" * 3)Compare: Addition vs. Multiplication—both are commutative and work across numeric types, but multiplication has higher precedence. If an exam question shows unexpected output from a mixed expression, check whether parentheses were needed to override default precedence.
Division in programming is trickier than in math class. Different division operators exist because programs need to handle both precise decimal results and whole-number-only scenarios. Understanding the distinction prevents countless bugs.
/ on integers, while Python 3 always returns floatx % 2 == 0 tests if a number is even; x % n cycles values through range 0 to n-1Compare: Division (/) vs. Integer Division (//) vs. Modulus (%)—these three operators answer different questions about the same division: what's the decimal result?, how many whole times does it fit?, and what's left over? FRQs often test whether you can pick the right one for a given scenario.
Raising numbers to powers appears constantly in algorithms, from calculating compound interest to implementing search algorithms. Not all languages use the same syntax, so know your language's approach.
2 ** 10 in Python and JavaScriptpow(base, exp) function or ^ operator (though ^ often means XOR)Compare: Exponentiation (**) vs. Multiplication (*)—both involve repeated operations conceptually, but exponentiation has higher precedence and is right-associative while multiplication is left-associative. Watch for expressions like 2 ** 3 ** 2 on exams.
These operators modify variables directly, either incrementing/decrementing by one or combining an operation with assignment. They're shortcuts that make code more concise but introduce evaluation subtleties.
x = x + 1 but more concise++x increments then returns the value; x++ returns then increments++; you must write x += 1 instead--x decrements first; x-- returns original value then decrementswhile (count-- > 0) is a classic pattern, but the timing can cause off-by-one errorsCompare: Prefix (++x) vs. Postfix (x++)—both add 1 to x, but they return different values in expressions. If x = 5, then y = ++x sets both to 6, while y = x++ sets y to 5 and x to 6. This distinction appears frequently in tracing exercises.
x += 5 is shorthand for x = x + 5, reducing repetition+=, -=, *=, /=, %=, **=, //= in most languages| Concept | Best Examples |
|---|---|
| Commutative operators | Addition (+), Multiplication (*) |
| Non-commutative operators | Subtraction (-), Division (/), Modulus (%) |
| Returns float by default | Division (/) |
| Returns integer | Integer division (//), Modulus (%) |
| Modifies variable in place | Increment (++), Decrement (--), Compound assignment (+=) |
| Right-associative | Exponentiation (**) |
| Causes runtime error if misused | Division by zero (/), Modulus by zero (%) |
| Prefix/postfix behavior | Increment (++), Decrement (--) |
Which two operators share the commutative property, and which division-related operator does not?
If x = 10, what is the difference in output between print(x++) and print(++x) in a language that supports both forms?
Compare and contrast / and // when applied to the expression divided by . What does each return, and why?
A student writes total = total + bonus * 2 but expected addition to happen first. What concept explains the actual behavior, and how would you fix the code?
You need to determine if a year is a leap year, which requires checking divisibility by 4, 100, and 400. Which operator would you use, and why is it better suited than division for this task?