Main content
AP®︎/College Computer Science Principles
Mathematical expressions
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:
operator | operation | example | result |
---|---|---|---|
+ | 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:
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. 😸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:
Instruction | Explanation |
---|---|
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:
Instruction | Explanation |
---|---|
a MOD b | Evaluates to the remainder when a is divided by b. Assumes that a and b are positive integers. |
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- "We can make longer expressions, like this one that calculates the number of hours in a year:
60 * 24 * 365
"
Isn't this the number of minutes in a year?(36 votes)- Thanks, fixing! By the way, you can use "Report a mistake" when you find typos in articles. It's a link underneath the text field box.(33 votes)
- Is this remainder the same mod as in clock arithmetic? (The one used in RSA encryption.) And what are some other applications of it...?(6 votes)
- Yes, the modulo and remainder operations work the same way. Some modulo operation uses are determining divisibility, days of the week, leap years, and the greatest common factor of two numbers (through the Euclidean Algorithm).(8 votes)
- Experiment with the arithmetic operators in this program:
println((77 - 32) * 5/9);
The above pseudocode displays a value of 25. After following the order of operations in this statement, I do not conclude the same value. Please help me read/calculate this statement correctly i.e. 77. - 32 = 45 then, 5 divided by 9 ?(2 votes)- (77 - 32) * 5/9
= 45 * 5/9
= 225/9
= 25(9 votes)
- What is the precedence of the modular operator?(4 votes)
- In remainder operators, what happens if a and b are negative integers or fractions?(3 votes)
- If a and b are non-integers, the remainder operation works exactly the same as with integers. a % b = remainder of a/b.
When the signs of a and b are different, a % b will equal n, where n = a + k*b (k is a positive, integer constant) and n lies within the interval [0,b] (if b is positive) or [b,0] (if b is negative).
When both a and b are negative, a % b will again equal the remainder of a/b except the remainder will now be in the interval [b, 0].(2 votes)
- It says that a-b represents b-a, so if it says 7-3 does it show the answer as -4?(2 votes)
- B SUBTRACTED from A is the same as saying a-b since you're subtracting b FROM a.(3 votes)
- what is the index of L in "Abraham Lincoln"(2 votes)
- I still can’t get to run the programmes. Doesn’t run in another tab as well.(0 votes)
- From the author:I do not know if our programming environment currently works on an iPad, it is very likely that it does not. However, you can follow the links underneath each program to hopefully find an environment that is iPad-friendly. Perhaps Code.org AppLab works on the iPad?(3 votes)
- it dont work(1 vote)