Computer Lab Report Essentials
Understanding Arithmetic Operators
The Building Blocks of Calculation
At its heart, a lot of programming is about working with numbers. To do that, we use special symbols called arithmetic operators. These are the familiar tools you've used in math class for years, just with a programming twist. They let you perform calculations, from simple sums to the logic that powers complex applications.
Let's start with the basics. The addition operator is the plus sign (+). It does exactly what you'd expect: it adds two numbers together.
let totalApples = 5 + 3; // totalApples is now 8
Similarly, the subtraction operator, the minus sign (-), finds the difference between two numbers.
let moneyLeft = 20 - 12; // moneyLeft is now 8
When it comes to multiplication, programmers use an asterisk (*) instead of the traditional '×' symbol. It calculates the product of two numbers.
let area = 4 * 6; // area is now 24
For division, we use a forward slash (/). It divides one number by another and gives you the quotient.
let slicePerPerson = 12 / 4; // slicePerPerson is now 3
Finding the Remainder
Now for an operator that might be new: the modulus operator (%). This operator doesn't give you the result of a division, but instead gives you what's left over. It finds the remainder.
Imagine you have 10 cookies to share equally among 3 friends. Each friend gets 3 cookies, and you have 1 cookie left over. The modulus operator tells you the value of that leftover part.
let leftoverCookies = 10 % 3; // leftoverCookies is now 1
The modulus operator is incredibly useful for tasks like checking if a number is even or odd. If a number % 2 is 0, the number is even. If it's 1, the number is odd.
Operator Summary
Here's a quick recap of the five basic arithmetic operators.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
% | Modulus | 7 % 2 | 1 |
Ready to test your knowledge? Give these questions a try.
In most programming languages, which symbol represents the multiplication operator?
What is the primary function of the modulus operator (%)?
Understanding these operators is the first step toward performing any kind of calculation in code.