Arithmetic Operators
By the end of this lesson, you'll be able to:
- Use +, -, *, /, and % correctly
- Explain what the modulo operator (%) actually calculates
- Predict the result of a multi-operator arithmetic expression using order of operations
Prerequisites: "What Is an Expression?"
The five operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 5 + 3 |
8 |
- |
Subtract | 5 - 3 |
2 |
* |
Multiply | 5 * 3 |
15 |
/ |
Divide | 5 / 3 |
1 (Integer division truncates — Module 3) |
% |
Modulo (remainder) | 5 % 3 |
2 |
What modulo is actually for
% gives you the remainder after division — genuinely useful for "does this divide evenly?" checks:
Integer orderNumber = 48214;
if (Math.mod(orderNumber, 2) == 0) {
System.debug('Even order number');
} else {
System.debug('Odd order number');
}
(Apex provides Math.mod() alongside the % operator for Integer values — both work, Math.mod() is the more explicit style many teams prefer.)
Order of operations
Apex follows the same PEMDAS rules as ordinary arithmetic: multiplication and division happen before addition and subtraction, and parentheses override everything.
Integer result = 2 + 3 * 4; // 14, not 20 — multiplication first
Integer result2 = (2 + 3) * 4; // 20 — parentheses force addition first
A real business example: Loan Processing
Decimal principal = 10000;
Decimal annualRatePercent = 6.5;
Decimal monthlyInterest = principal * (annualRatePercent / 100) / 12;
System.debug(monthlyInterest); // 54.1666...
A basic monthly interest calculation combines multiplication and division in exactly the order-of-operations pattern this lesson covers.
Exercise
Declare Integer variables a = 17 and b = 5. Debug their sum, difference, product, quotient, and remainder (five separate debug statements).
Show hint
Remainder uses %: a % b.
Arithmetic Operators Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Apex's five arithmetic operators — add, subtract, multiply, divide, and modulo — follow the same order-of-operations rules you learned in school, and modulo (the one genuinely new one) turns out to be surprisingly useful in real business logic.