Welcome to Mathematical Expressions!
Ever wonder how a video game calculates your high score, or how a social media app determines how many likes a post has? It all comes down to Mathematical Expressions. In Computer Science Principles, math isn't just about solving for \(x\); it is about giving the computer instructions to process data and solve problems. Don't worry if you aren't a "math person"—computer math follows a very specific set of rules that are easy to master once you see the patterns!
Note: This chapter focuses on how the computer handles numbers. To learn how to store these results, see our chapter on Variables and Assignments.
1. The Basic Arithmetic Operators
The AP CSP Exam uses four standard operators that you likely already know from school. However, there is one very important rule about division you need to remember!
The Four Basic Operators:
1. Addition: \(a + b\)
2. Subtraction: \(a - b\)
3. Multiplication: \(a * b\) (We use the asterisk symbol for multiplication!)
4. Division: \(a / b\)
The Golden Rule of Division:
In some programming languages, dividing two whole numbers gives you a whole number. But on the AP CSP Exam, division is NOT integer division. This means you keep the decimal!
Example: \(17 / 5\) evaluates to \(3.4\).
Quick Review:
If the exam asks for \(7 / 2\), the answer is \(3.5\), not \(3\)!
2. The MOD Operator (The Remainder Finder)
The MOD operator (short for "modulo") is often the newest concept for students, but it is incredibly useful. It is written as \(a \text{ MOD } b\).
What it does: It tells you the remainder left over after dividing \(a\) by \(b\).
The Constraints: For the exam, \(a\) must be an integer greater than or equal to \(0\), and \(b\) must be an integer greater than \(0\).
Step-by-Step Example: \(17 \text{ MOD } 5\)
1. Think: How many times does \(5\) fit into \(17\) fully?
2. \(5 \times 3 = 15\). (It fits \(3\) times).
3. What is left over? \(17 - 15 = 2\).
4. So, \(17 \text{ MOD } 5 = 2\).
Step-by-Step Example: \(3 \text{ MOD } 10\)
1. Think: How many times does \(10\) fit into \(3\)?
2. It fits \(0\) times! \(10 \times 0 = 0\).
3. What is left over? \(3 - 0 = 3\).
4. So, \(3 \text{ MOD } 10 = 3\). (Trick: If the first number is smaller than the second, the answer is just the first number!)
Real-World Analogy:
Think of a clock. If it is 10:00 and you add 5 hours, it becomes 3:00. This is "Clock Math" or Modular Arithmetic! We use \(15 \text{ MOD } 12\) to find the hour on the clock.
Why do we use MOD?
1. Even or Odd: Any number \(\text{ MOD } 2\) will result in \(0\) if it is even and \(1\) if it is odd.
2. Divisibility: If \(a \text{ MOD } b = 0\), then \(a\) is perfectly divisible by \(b\).
3. Order of Operations
Just like in math class, computers follow a specific order when solving expressions. If you don't follow these, you'll get the wrong answer!
The AP CSP Priority List:
1. Parentheses: Anything inside \( ( ) \) happens first.
2. Multiplication, Division, and MOD: These three have the same level of importance. You solve them from left to right as they appear.
3. Addition and Subtraction: These have the lowest priority. Solve them from left to right after the others are done.
Example Walkthrough:
Evaluate: \(10 + 5 * 4 \text{ MOD } 3\)
1. Multiplication first: \(5 * 4 = 20\). Our expression is now \(10 + 20 \text{ MOD } 3\).
2. MOD next (same priority as multiplication): \(20 \text{ MOD } 3 = 2\) (because \(3 * 6 = 18\), and \(20 - 18 = 2\)).
3. Addition last: \(10 + 2 = 12\).
Final Result: \(12\)
Common Mistake Alert:
Don't assume addition comes before MOD just because MOD looks like a word! Treat MOD with the same "strength" as Multiplication and Division.
4. Key Takeaways and Tips
Key Terms to Remember:
- Expression: A combination of values and operators that the computer evaluates to a single value.
- Operator: Symbols like \(+\), \(-\), \(*\), \(/\), and \(\text{MOD}\) that perform actions on numbers.
Quick Summary:
- Multiplication uses the \(*\) symbol.
- Division (\(/\)) always includes decimals (e.g., \(5 / 2 = 2.5\)).
- MOD is the remainder after division.
- Follow the order: \( ( ) \) first, then \(*\), \(/\), \(\text{MOD}\), then \(+\), \(-\).
Did you know?
Computer scientists use the MOD operator to help with "wraparound" effects in games. If a character walks off the right side of a screen (position 100) and you want them to appear on the left (position 0), MOD is the tool for the job!
Final Encouragement:
When you see a long string of math on the exam, don't panic! Take it one step at a time, underline the operator with the highest priority, and solve it piece by piece. You've got this!