Arithmetic Operations in a Programming Language
Hello future programmers! Welcome to the exciting world of calculations in code. Whether you are building a game that scores points or a financial program that manages budgets, your computer needs to do math!
This chapter will teach you the essential tools and rules for performing calculations. Don't worry if maths isn't your favorite subject—we are learning the computer's way of doing math, and it's much simpler than advanced algebra!
What We Will Learn:
- The basic operators (+, -, *, /)
- The special operators: Integer Division (DIV) and Modulus (MOD)
- The critical rule of calculation order: Operator Precedence
Section 1: The Fundamental Arithmetic Operators
Most programming languages use symbols that look very similar to the ones you use in everyday mathematics, but there are a couple of small differences you must know.
The computer processes these operations from left to right, unless a specific rule (precedence) tells it otherwise (more on that later!).
Basic Operators Table
| Operator | Meaning | Example in Code | Result |
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 12 - 4 | 8 |
| * | Multiplication | 6 * 7 | 42 |
| / | Division (Standard) | 10 / 4 | 2.5 (a decimal/float) |
Key Takeaway: The asterisk (*) is always used for multiplication, and the forward slash (/) is for standard division, which usually results in a decimal number (a float).
Section 2: The Special Operations: DIV and MOD
In programming, we often need to deal only with whole numbers. This is where the standard division (/) isn't enough. We need two special operators: Integer Division (DIV) and Modulus (MOD).
Analogy: Imagine you have 17 sweets and you want to share them equally among 5 friends. How many whole sweets does each person get? And how many are left over?
1. Integer Division (DIV)
The Integer Division operator (often written as DIV or //) performs division and throws away the remainder, giving you only the whole number part of the result.
- Purpose: To find out how many times one number fits fully into another.
- Example: 17 DIV 5
- Calculation: 5 goes into 17 three full times (3 x 5 = 15).
- Result: 3
2. Modulus (MOD)
The Modulus operator (often written as MOD or %) performs division and gives you only the remainder.
- Purpose: To find out what is left over after a full division. This is extremely useful for checking if a number is even/odd, or for timing (e.g., converting seconds into minutes and remaining seconds).
- Example: 17 MOD 5
- Calculation: 17 divided by 5 is 3, with 2 left over (17 - 15 = 2).
- Result: 2
Memory Aid:
DIV gives you the Dollars (the main whole amount).
MOD gives you the Money left over (the remainder).
Quick Review: DIV and MOD
Let's try 25 DIV 6 and 25 MOD 6:
- 6 goes into 25 four times (4 x 6 = 24).
- 25 DIV 6 = 4 (The whole number result).
- The remainder is 1 (25 - 24 = 1).
- 25 MOD 6 = 1 (The remainder).
Did you know? Programmers use MOD to check if a number is even. If Number MOD 2 equals 0, the number is even!
Section 3: Operator Precedence (The Order of Operations)
If you have an expression like \(5 + 3 \times 2\), does the computer do \(5+3\) first, or \(3 \times 2\) first? The answer is determined by Operator Precedence—the set of rules that defines the order in which operations are performed.
If the computer didn't follow a strict order, two different people running the same code could get two different answers! We need consistency.
The rules of precedence are the same rules you learned in math, usually remembered by the acronym BODMAS (Brackets, Orders, Division/Multiplication, Addition/Subtraction) or PEMDAS.
The Programming Precedence Hierarchy (Highest to Lowest)
- Brackets (Parentheses) ( )
Operations inside brackets are always calculated first. You can use brackets to force the calculation order you want.
- Orders (Exponents or Powers)
Although power functions (like \(2^3\)) might be written differently (e.g.,
**or^), these operations happen before standard multiplication/division. - Multiplication (*), Standard Division (/), Integer Division (DIV), Modulus (MOD)
These four operations have equal precedence. They are calculated next, working strictly from left to right across the equation.
- Addition (+) and Subtraction (-)
These have the lowest precedence. They are calculated last, also working from left to right.
Step-by-Step Example Walkthrough
Let's calculate the result of the following expression:
Result = 10 + 2 * (15 MOD 4) - 1
- Brackets First: We must solve (15 MOD 4).
- 15 divided by 4 is 3 remainder 3.
- 15 MOD 4 = 3
The expression becomes: 10 + 2 * 3 - 1
- Multiplication/Division Next: We have 2 * 3.
- 2 * 3 = 6
The expression becomes: 10 + 6 - 1
- Addition/Subtraction Last (Left to Right):
- 10 + 6 = 16
- 16 - 1 = 15
Final Result: 15
Common Mistake to Avoid!
If you want to add numbers before multiplying them, you MUST use brackets.
Incorrect: 5 + 2 * 10 → Result is 25 (because 2*10 happens first)
Correct: (5 + 2) * 10 → Result is 70 (because 5+2 happens first)
Key Takeaway: When in doubt, use brackets ( )! They clearly tell the computer (and anyone reading your code) exactly which operation should happen first.
Section 4: Calculations with Variables
In programming, calculations rarely involve only fixed numbers. Usually, we use variables to store values that can change. The rules of arithmetic and precedence apply exactly the same way to variables as they do to raw numbers.
Example using Variables
Imagine we have variables storing information about a basket of apples:
NumApples = 20
CostPerApple = 0.50
Discount = 2
To calculate the final price after a discount, a programmer might write:
TotalPrice = (NumApples * CostPerApple) - Discount
Step-by-Step Variable Calculation:
- Brackets First: (20 * 0.50) → 10.00
- Subtraction Last: 10.00 - 2 → 8.00
TotalPricenow holds the value 8.00.
Using MOD for Practical Problems
A classic exam-style question involves time conversion. If we have 300 seconds, how many full minutes is that, and how many seconds are left over?
TotalSeconds = 300
Minutes = TotalSeconds DIV 60
RemainingSeconds = TotalSeconds MOD 60
Calculation:
Minutes = 300 DIV 60 → 5 (5 full minutes)
RemainingSeconds = 300 MOD 60 → 0 (0 seconds left over)
This shows how DIV and MOD work together perfectly to split a total quantity (seconds) into the whole units (minutes) and the remaining partial units (remaining seconds).
Accessibility Tip: Breaking Down Precedence
If a calculation looks scary (e.g., \(4 \times (2 + 3) \text{ DIV } 2 + 1\)), write it down and circle the operation with the highest precedence first. Solve that part, replace it with the result, and repeat until the end. This prevents confusion!
Final Key Takeaway: Arithmetic operations are the backbone of programming. Mastering DIV, MOD, and Operator Precedence ensures your programs calculate results accurately every time!