4.0 Making Things Happen: Using Operators
Operators are special symbols that tell the compiler to perform a specific mathematical or logical action on your variables.
4.1 The Assignment Operator:
The single equals sign (=) is the most fundamental operator. Its job is to assign a value to a variable. It takes the value on its right and stores it in the variable on its left.
For example, ledPin = 13; takes the number 13 and puts it into the variable named ledPin.
4.2 Doing Math: Arithmetic Operators
These operators perform standard mathematical calculations.
- + (Addition): Adds two values together.
- – (Subtraction): Subtracts the second value from the first.
- * (Multiplication): Multiplies two values.
- / (Division): Divides the first value by the second.
- % (Modulo): Gives you the remainder after an integer division (e.g., 10 % 3 would result in 1).
4.3 Asking Questions: Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is always a boolean value: either true or false. They are essential for making decisions in your code.
Crucial Note: A very common mistake for beginners is to confuse the assignment operator (=) with the “is equal to” comparison operator (==). Remember, a single equals sign assigns a value, while a double equals sign compares two values.
| Operator | Meaning | Example |
| == | Is equal to? | x == 10 |
| != | Is not equal to? | x != 10 |
| < | Is less than? | x < 10 |
| > | Is greater than? | x > 10 |
| <= | Is less than or equal to? | x <= 10 |
| >= | Is greater than or equal to? | x >= 10 |
Let’s see how all these concepts—structure, variables, data types, and operators—come together in a simple, working program.