If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Lesson 3: Math

# Mathematical expressions

AP.CSP:
AAP‑2 (EU)
,
AAP‑2.C (LO)
,
AAP‑2.C.1 (EK)
,
AAP‑2.C.2 (EK)
,
AAP‑2.C.4 (EK)
Computers are incredibly good at math. In fact, that's why computers were originally invented! Mathematicians and physicists used to spend hours calculating results from their formulas and data. Now, a computer can "compute" results for them in a fraction of a second.
So how do we actually get the computer to do math for us?

## Arithmetic operators

Programming languages come with arithmetic operators, and we can use those to create mathematical expressions.
The JavaScript language provides these operators:
operatoroperationexampleresult
`+`addition`33 + 67``100`
`-`subtraction`1024 - 256``768`
`*`multiplication`12 * 12``144`
`/`division`144 / 12``12`
`%`remainder`10 % 3``1`
Many of those likely look familiar, and are the same operations you use calculators for in math class. However, most new programmers have never seen `%`, the remainder operator.
The expression `10 % 3` calculates the remainder of 10 divided by 3. The result is 1, since 10 / 3 equals 3 and leaves a remainder of 1.
The remainder operator is often called the "modulo" operator, so we typically read the expression `10 % 3` as "10 mod 3".

### Multiple operators

We can make longer expressions, like this one that calculates the number of minutes in a year:
``60 * 24 * 365``
We can also combine different operators in the same expression, like this one that converts the temperature of 77°F to Celsius:
``(77 - 32) * 5/9``
When an expression involves different operators, JavaScript follows an order of operations for deciding what operations to evaluate first, like multiplying before subtracting. However, it's often best to use parentheses to make the order obvious and reduce the likelihood of making a mistake.
🔍Try it yourself: Experiment with the arithmetic operators in this program:
📝 See equivalent code in: App Lab | Snap | Python

## Storing with variables

We'll often want to store the results of mathematical expressions in variables, especially if we want to reuse the results of a calculation later.
This line of JavaScript stores the number of milliseconds in 1 hour:
``var hourMS = 1000 * 60 * 60;``
Once that line runs, the variable `hourMS` stores a value of `3600000` and we can reference that variable later in our program.
We can also use variables inside mathematical expressions, as long as those variables store numbers.
This code calculates the flour needed to bake 2 loaves of bread:
``````var numLoaves = 2;
var totalFlourCups = numLoaves * 4;``````
When the computer sees `numLoaves`, it looks up its value, sees that it stores the number 2, and then happily calculates `2 * 4`, storing a final result of `8` in the variable `totalFlourCups`.
The program below calculates the daily range of calories for my cat. The final `minCal` and `maxCal` expressions operate entirely on variables.
🔍 Try changing the `catWeight` variable to store a different weight (your own cat's weight, if you have one), and see how the results change. 😸
📝 See equivalent code in: App Lab | Snap | Python

## Pseudocode for mathematical expressions

The majority of programming languages use the same operators for basic arithmetic: `+`, `-`, `*`, `/`.
That's also how we represent them in pseudocode:
InstructionExplanation
`a + b`Evaluates to the result of `b` added to `a`
`a - b`Evaluates to the result of `b` subtracted from `a`
`a * b`Evaluates to the result of `a` multiplied by `b`
`a / b`Evaluates to the result of `a` divided by `b`
The remaining operator is the remainder operator, which varies more across languages. Here's how we represent it in our pseudocode:
InstructionExplanation
a `MOD` bEvaluates to the remainder when a is divided by b. Assumes that `a` and `b` are positive integers.