Sub topic 6
Operators and Expressions
Assume you can open, verify, and upload an Arduino sketch, and that you understand setup(), loop(), variables, and basic digital input and output. This chapter uses those ideas to calculate values and make decisions.
An operator is a symbol that performs an action. An expression combines values and operators to produce a result. For example, temperature + 2 is an expression.
Arithmetic operators
Arithmetic operators calculate numbers.
| Operator | Meaning | Example |
|---|---|---|
| `+` | addition | `a + b` |
| `-` | subtraction | `a - b` |
| `*` | multiplication | `a * b` |
| `/` | division | `a / b` |
| `%` | remainder | `a % b` |
The % operator is useful for testing whether a number is even or for repeating a pattern. For example, count % 2 == 0 is true when count is even.
Integer division drops the fractional part. If both operands are integers, 5 / 2 produces 2, not 2.5. Use a floating-point value when you need a fractional result:
int whole = 5 / 2;
float precise = 5.0 / 2.0;
void setup() {
Serial.begin(9600);
Serial.println(whole);
Serial.println(precise);
}
void loop() {
}Assignment and comparison
The assignment operator = stores a value:
int level = 10;
level = level + 5;The comparison operator == asks whether two values are equal. Confusing = with == is a common error.
Other comparison operators are:
!=means not equal.<means less than.>means greater than.<=means less than or equal to.>=means greater than or equal to.
A comparison produces a Boolean result. A Boolean is a value that is either true or false.
int sensorValue = analogRead(A0);
if (sensorValue >= 600) {
Serial.println("Bright");
} else {
Serial.println("Dim");
}Logical and compound operators
Logical operators combine Boolean expressions:
&&means AND. Both expressions must be true.||means OR. At least one expression must be true.!means NOT. It reverses the result.
if (sensorValue >= 300 && sensorValue <= 700) {
Serial.println("In the middle range");
}The compound assignment operators make updates shorter:
count += 1; // Same as count = count + 1
count -= 2;
count *= 3;
count /= 4;The increment operators ++ and -- add or subtract one:
count++;
count--;Evaluation order
Arduino C++ follows precedence rules. Multiplication and division normally happen before addition and subtraction:
int result = 2 + 3 * 4; // 14Use parentheses when the intended order matters:
int result = (2 + 3) * 4; // 20Parentheses also make code easier to read. Do not rely on remembering every precedence rule in a complicated expression. Break it into named variables instead.
Operators in a running sketch
This sketch combines arithmetic, comparison, logical operators, and assignment. It reads a sensor, maps the reading into a smaller range, and reports whether the result is in an acceptable range.
const byte sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int rawValue = analogRead(sensorPin);
int percent = rawValue * 100 / 1023;
bool acceptable = percent >= 30 && percent <= 70;
Serial.print("Raw: ");
Serial.print(rawValue);
Serial.print(", percent: ");
Serial.print(percent);
Serial.print(", acceptable: ");
Serial.println(acceptable ? "yes" : "no");
delay(500);
}The conditional operator condition ? valueIfTrue : valueIfFalse selects one of two values. It is useful for short choices, but use if and else when the logic becomes harder to scan.
Exercise
What value does int result = 18 / 4 + 2 * 3; store, and how could you make the calculation produce 7.5?
Show answer
It stores 10 because integer division gives 4, then 2 * 3 gives 6, and 4 + 6 gives 10. Use floating-point operands and a float, such as float result = 18.0 / 4.0 + 2.0 * 3.0;, to produce 10.5. To produce 7.5, use parentheses: float result = (18.0 / 4.0) + 3.0;.