Modulo in the Order of Operations
The modulo operator (%) has the same precedence as multiplication and division in the standard order of operations. This means that in an expression like 10 + 15 % 4 * 2, the modulo and multiplication are evaluated left-to-right before the addition.
Operator Precedence Table
| Priority | Operators | Description | Associativity |
|---|---|---|---|
| 1 (highest) | ( ) |
Parentheses / Grouping | N/A |
| 2 | ** |
Exponentiation | Right-to-left |
| 3 | * / % |
Multiplication, Division, Modulo | Left-to-right |
| 4 (lowest) | + - |
Addition, Subtraction | Left-to-right |
Step-by-Step Examples
10 + 15 % 4 * 2
1. Evaluate 15 % 4 = 3 (same level as *)
2. Evaluate 3 * 2 = 6 (left-to-right)
3. Evaluate 10 + 6 = 16
(10 + 15) % 4 * 2
1. Parentheses first: 10 + 15 = 25
2. Evaluate 25 % 4 = 1
3. Evaluate 1 * 2 = 2
20 % 7 + 3 * 2
1. Evaluate 20 % 7 = 6
2. Evaluate 3 * 2 = 6
3. Evaluate 6 + 6 = 12
100 % 30 % 7
1. Left-to-right: 100 % 30 = 10
2. Then: 10 % 7 = 3
Note: same-level operators are left-to-right.
Common Misconceptions
Misconception 1: Modulo Has Lower Precedence Than Multiplication
Some people mistakenly believe that modulo has a lower precedence than multiplication or division. In reality, modulo (%) has the exact same precedence as * and /, and they are all evaluated left-to-right.
Misconception 2: PEMDAS/BODMAS Does Not Include Modulo
The traditional PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction) mnemonic does not mention modulo because it was designed for basic arithmetic. However, in programming and extended mathematics, modulo sits at the same level as the M and D (Multiplication and Division).
Using Parentheses for Clarity
When in doubt, use parentheses to make your intent explicit. Even if you know the precedence rules, parentheses make expressions easier to read and less error-prone:
10 + (15 % 4) * 2makes it clear that % is evaluated first(10 + 15) % (4 * 2)groups addition and multiplication before modulo
Modulo Precedence in Programming Languages
The % operator has the same precedence as * and / in virtually all major programming languages, including JavaScript, Python, C, C++, Java, C#, Go, Rust, Ruby, PHP, and Swift. This consistency makes the rule easy to remember across different languages.