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.

Main content

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
+addition33 + 67100
-subtraction1024 - 256768
*multiplication12 * 12144
/division144 / 1212
%remainder10 % 31
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 + bEvaluates to the result of b added to a
a - bEvaluates to the result of b subtracted from a
a * bEvaluates to the result of a multiplied by b
a / bEvaluates 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.

🙋🏽🙋🏻‍♀️🙋🏿‍♂️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?

  • purple pi pink style avatar for user Devan Eilbert
    "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?
    (26 votes)
    Default Khan Academy avatar avatar for user
  • leafers tree style avatar for user Chawin S.
    Is this remainder the same mod as in clock arithmetic? (The one used in RSA encryption.) And what are some other applications of it...?
    (4 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Laxton Milanzie
    In remainder operators, what happens if a and b are negative integers or fractions?
    (2 votes)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      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].
      (1 vote)
  • blobby green style avatar for user roniwind9491
    It says that a-b represents b-a, so if it says 7-3 does it show the answer as -4?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user London Brown
    How do we know when can we answer a math problem?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user NAVEED RIAZ
    I still can’t get to run the programmes. Doesn’t run in another tab as well.
    (0 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      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?
      (0 votes)
  • hopper cool style avatar for user Digital
    how do i use something like math.evaluate() or eval() in your program?
    (0 votes)
    Default Khan Academy avatar avatar for user