Beginner 15 min read

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');
}
  1. Integer orderNumber = 48214; — the number being checked.
  2. if (Math.mod(orderNumber, 2) == 0) { — Math.mod(orderNumber, 2) gives the remainder after dividing by 2 — 0 means the number divides evenly (even), anything else means it doesn't (odd).
  3. System.debug('Even order number'); — runs only if line 2 is true.
  4. } else { — closes the if block and opens the alternative path.
  5. System.debug('Odd order number'); — runs when line 2 is false.
  6. } — closes the else block.

(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
  1. Integer result = 2 + 3 * 4; — 3 * 4 runs first (multiplication before addition), giving 2 + 12 = 14.
  2. Integer result2 = (2 + 3) * 4; — the parentheses force 2 + 3 to run first, giving 5 * 4 = 20.

A real business example: Loan Processing

Decimal principal = 10000;
Decimal annualRatePercent = 6.5;
Decimal monthlyInterest = principal * (annualRatePercent / 100) / 12;
System.debug(monthlyInterest); // 54.1666...
  1. Decimal principal = 10000; — the loan amount.
  2. Decimal annualRatePercent = 6.5; — the yearly interest rate, as a percentage.
  3. Decimal monthlyInterest = principal * (annualRatePercent / 100) / 12; — the parentheses convert the percentage to a decimal fraction first (6.5 / 100 = 0.065), then multiplies by principal, then divides by 12 months.
  4. System.debug(monthlyInterest); — logs the result, 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Arithmetic Operators Quiz

1. What does the % operator calculate?

2. In "2 + 3 * 4", multiplication happens before addition.

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

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.