Welcome to the World of Computer Calculations!
In this chapter, we are going to explore Arithmetic Operations. While that might sound like a fancy title, it really just means "doing math in code." Whether you are calculating a high score in a game, working out the price of items in a shopping cart, or figuring out how many players can fit into a team, you will need these operations.
Don't worry if math isn't your favorite subject! In programming, the computer does the hard work of calculating; you just need to know which symbol to use and when. Let's dive in!
1. The Basics: Addition, Subtraction, and Multiplication
If you’ve ever used a calculator, you already know most of these! These operations work exactly like they did in primary school, but the symbols might look slightly different on a keyboard.
Addition (+)
Used to add values together.
Example: score + 10
Subtraction (-)
Used to take one value away from another.
Example: health - 5
Multiplication (*)
In programming, we don't use an 'x' for multiplication because the computer might think it's a letter. Instead, we use the asterisk (*) symbol.
Example: total_price = price * quantity
Quick Review Box:
• Addition: \( + \)
• Subtraction: \( - \)
• Multiplication: \( * \)
2. The Two Faces of Division
This is where things get interesting! In the AQA syllabus, there are three ways to talk about division. It helps to think about "sharing" vs. "filling containers."
Real Division (/)
This is "normal" division. It gives you the full answer, including the decimal bits (called Real or Float numbers).
Example: \( 11 / 2 = 5.5 \)
Integer Division (DIV)
The word Integer means a whole number. DIV tells you how many whole times a number goes into another. It completely ignores the remainder or the decimal part.
Analogy: Imagine you have 11 eggs and a carton that holds 2 eggs. How many full cartons do you have? You have 5 full cartons.
Example: \( 11 \text{ DIV } 2 = 5 \)
The Remainder or Modulo (MOD)
MOD (or Modular Arithmetic) only cares about what is left over after a division has happened.
Analogy: Using the egg example again—if you put 11 eggs into cartons of 2, you have 1 egg left over that didn't fit into a full carton. That leftover egg is the MOD.
Example: \( 11 \text{ MOD } 2 = 1 \)
Did you know?
MOD is incredibly useful for checking if a number is even or odd! If you use \( \text{number MOD } 2 \) and the result is 0, the number is even. If the result is 1, the number is odd.
Key Takeaway:
Real Division (/) = The whole decimal answer.
DIV = How many whole times it fits.
MOD = What is left over (the remainder).
3. Step-by-Step: Solving a Division Problem
When you see a calculation like \( 17 / 5 \), you can break it down into the two parts the syllabus expects you to know:
Step 1: Find the DIV
Ask yourself: "How many times does 5 fit into 17?"
5, 10, 15... it fits 3 times.
So, \( 17 \text{ DIV } 5 = 3 \).
Step 2: Find the MOD
Ask yourself: "After taking away that 15, what is left over?"
\( 17 - 15 = 2 \).
So, \( 17 \text{ MOD } 5 = 2 \).
Don't worry if this seems tricky at first! Just remember: DIV is for the "Big Whole Number" and MOD is for the "Leftovers."
4. Common Mistakes to Avoid
• Using 'x' for multiply: Always use the * symbol in your code or exam answers.
• Confusing DIV and MOD: A simple trick is to remember MOD stands for "Maybe Over Done" (the bits you couldn't finish) or simply M for Modulo and R for Remainder.
• Dividing by Zero: Computers (and math) don't like this! Trying to calculate \( 10 / 0 \) will usually cause a program to crash.
5. Summary Table for Revision
Use this table to quickly check the symbols and meanings before your exam.
Operation: Addition | Symbol: + | Example: \( 5 + 2 = 7 \)
Operation: Subtraction | Symbol: - | Example: \( 5 - 2 = 3 \)
Operation: Multiplication | Symbol: * | Example: \( 5 * 2 = 10 \)
Operation: Real Division | Symbol: / | Example: \( 5 / 2 = 2.5 \)
Operation: Integer Division | Symbol: DIV | Example: \( 5 \text{ DIV } 2 = 2 \)
Operation: Remainder | Symbol: MOD | Example: \( 5 \text{ MOD } 2 = 1 \)
Final Key Takeaway:
Arithmetic operations allow programs to process numerical data. While +, -, and * are straightforward, DIV and MOD are a "two-stage process" used to find the quotient and the remainder separately.