Arithmetic Operators
What you'll learn: How to use basic math operators to perform calculations with numbers in code.
What Are Arithmetic Operators?
Arithmetic operators are symbols that tell the computer to perform mathematical calculations on numeric values (integers and floating-point numbers). They're the building blocks for any computation in your programs—from simple addition to complex formulas.
The Five Core Operators
Just like in mathematics, programming languages provide operators for basic arithmetic:
-
Addition (
+): Adds two numbers together- Example:
5 + 3evaluates to8
- Example:
-
Subtraction (
-): Subtracts the second number from the first- Example:
10 - 4evaluates to6
- Example:
-
Multiplication (
*): Multiplies two numbers- Example:
7 * 6evaluates to42
- Example:
-
Division (
/): Divides the first number by the second- Example:
20 / 4evaluates to5
- Example:
-
Modulo (
%): Returns the remainder after division- Example:
17 % 5evaluates to2(because 17 ÷ 5 = 3 remainder 2)
- Example:
Using Operators in Expressions
Remember that expressions (which you just learned about) combine values and operators. Arithmetic operators are what make numeric expressions powerful:
totalCost = price * quantity
remainder = minutes % 60
average = (score1 + score2 + score3) / 3
Each operator takes two operands (the values on either side) and produces a result that becomes part of the expression's final value.
Key Takeaway: Arithmetic operators (+, -, *, /, %) let you perform mathematical calculations on numeric data, turning your variables and values into useful computed results.