Computer Science
Grade 9
20 min
Basic Math Operations
Basic Math Operations
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Correctly apply the order of operations (PEMDAS/BODMAS) in complex programming expressions.
Differentiate between standard division and integer (floor) division and use each appropriately.
Utilize the modulo operator to solve problems involving remainders, such as determining if a number is even or odd.
Implement compound assignment operators (e.g., +=, *=) to write more concise and efficient code.
Analyze and debug code that involves multiple mathematical operations.
Convert numerical data between integer and floating-point types to ensure calculation accuracy.
Ever wonder how a game knows exactly when to wrap a character around the screen or how an app can split a bill perfectly down to the cent? 👾 It's all about mastering the 'advanced'...
2
Key Concepts & Vocabulary
TermDefinitionExample
Operator PrecedenceThe specific order in which a programming language performs mathematical and logical operations in an expression. It's the computer's version of PEMDAS/BODMAS.In the expression `10 + 5 * 2`, multiplication is performed before addition, so the result is `20`, not `30`.
Integer Division (Floor Division)A type of division that rounds the result down to the nearest whole number, effectively discarding any fractional part or remainder.In Python, `14 // 3` results in `4`, not `4.666...`. The `.666...` is discarded.
Modulo OperatorAn operator that performs division but returns only the remainder. It's incredibly useful for checking divisibility and creating cycles.In most languages, `14 % 3` results in `2`, because 14 divided by 3 is 4 with...
3
Core Syntax & Patterns
Order of Operations in Code
1. Parentheses `()`
2. Exponentiation `**`
3. Multiplication `*`, Division `/`, Floor Division `//`, Modulo `%` (from left to right)
4. Addition `+`, Subtraction `-` (from left to right)
Use this hierarchy to predict the outcome of any complex mathematical expression. When in doubt, use parentheses `()` to enforce the order you want.
The Modulo and Integer Division Relationship
`a = (a // b) * b + (a % b)`
This formula shows how a number `a` can be reconstructed from its integer division and modulo with respect to another number `b`. It's the fundamental principle behind how these two operators work together.
Compound Assignment Syntax
`variable op= value`
This is the standard syntax for compound assignment, where `op` can be `+`, `-`...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You are distributing 100 students as evenly as possible into `N` buses. The number of students on most buses will be `100 // N`. Some buses will need to take one extra student to account for the remainder. Which expression calculates exactly how many buses will need to take an extra student?
A.100 // N
B.100 % N
C.(100 % N) + 1
D.N - (100 % N)
Challenging
A number `a` was operated on with a number `b=9`. The result of the integer division `a // b` was `11`, and the result of the modulo operation `a % b` was `5`. What was the original number `a`?
A.99
B.55
C.104
D.16
Challenging
A game's turn alternates between 4 players, numbered 1, 2, 3, and 4. The game starts with `turn_number = 1` for Player 1. Which expression correctly determines the current player's number for any given `turn_number` (where `turn_number >= 1`)?
A.turn_number % 4
B.(turn_number % 4) + 1
C.(turn_number + 3) % 4
D.(turn_number - 1) % 4 + 1
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free