Mathematical
Expressions
Learn how programming languages evaluate arithmetic, use operators, and handle the order of operations to build powerful algorithms.
What Is a Mathematical Expression?
In programming, a mathematical expression is a combination of values, variables, and operators that the computer evaluates to produce a single result. Just like in algebra, expressions follow specific rules to determine the final value.
The Building Blocks of Expressions
Why Do Expressions Matter?
Every algorithm relies on mathematical expressions to transform data. Whether you're calculating a game score, converting temperatures, or analyzing data, expressions are the engine that drives computation.
The Arithmetic Operators
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 7 + 3 | 10 |
| Subtraction | - | 7 - 3 | 4 |
| Multiplication | * | 7 * 3 | 21 |
| Division | / | 7 / 2 | 3.5 |
| Modulus | MOD | 7 MOD 3 | 1 |
Order of Operations
Computers follow a strict precedence hierarchy when evaluating expressions, just like PEMDAS in math. Higher-precedence operators are evaluated first.
Visualizing Precedence
Integer vs. Float Division
The MOD Operator Deep Dive
How to Evaluate Any Expression
Example 1: Simple Expression
Example 2: Parentheses Change Everything
Example 3: MOD in Action
Full days elapsed: the division gives a decimal result.
After 1 full day (24h), there are 3 hours left over.
Assignment vs. Equality
Evaluate the right side, then store the result in x. If x was 5, it becomes 6.
A comparison that asks "Is x equal to 5?" Returns true or false.
Common Mistakes to Avoid
Summary: Operator Reference
| Concept | Syntax | Precedence | Key Note |
|---|---|---|---|
| Parentheses | ( ) | Highest | Override all other rules |
| Multiplication | * | Medium | Left to right with / and MOD |
| Division | / | Medium | Returns decimal (real division) |
| Modulus | MOD | Medium | Returns integer remainder |
| Addition | + | Lowest | Left to right with - |
| Subtraction | - | Lowest | Left to right with + |
| Assignment | ← | After eval | Right side evaluated first |
Question 1 of 5
What is the value of result after this code runs?
result ← 5 + 3 * 4 - 2
Question 2 of 5
What does 20 MOD 6 evaluate to?
Question 3 of 5
Given x ← 4, what is the value of x after executing x ← x * x + 1?
Question 4 of 5
What is the value of result?
result ← (10 + 2) / (3 * 2)
Question 5 of 5
Which expression correctly checks if a number n is even?