You use operators to link variables, statements and functions together. You'll recognize some of these operators from arithmetic and math. We cover four types of operators here: arithmetic, comparison, Boolean and compound. All of these operators can be used in C++, too.
Arithmetic in code works exactly like arithmetic on paper, but you have more value-types available to you. If you want to divide a sensor reading by 4 and use that value in analogRead, for example, you can use:
and the arithmetic will happen using order of operations.
= assignment operator: value = value
Set two things equal to each other. This can be a variable equal to a number or a variable equal to a variable
+ addition: value + value
Add two things together. Variable + variable, number + number, variable + number, sensor reading + variable…
- subtraction: value - value
Subtract one value from another. Values can be variables, numbers, or sensor readings.
* multiplication: value * value
Multiply two values together. Values can be variables, numbers, sensor readings, results of other arithmetic.
/ division: value / value
Divide one value by another. You can use numbers, variables, other arithmetic, sensor readings, or pin numbers as your values.
% modulo: value % value
Modulo is a concept you don't use often in normal arithmetic. It divides two values, then returns to you the remainder of that division. You can use it with whole numbers or whole-number variables (ints, longs, bytes). Modulo is useful when you want to return, for example the 1's place of a temperature value. Assume you know it is in the 70s, but you don't know exactly what temperature it is. TempVariable % 10 divides your tempVariable by 10 and then returns the remainder of that division. If tempVariable was equal to 76, then tempVariable % 10 would return 6.
== equal to: value == value;
The 'equal to' comparator is two equal signs back-to-back. It is not setting two things equal, but is returning 'true' if two things are equal. You will often want to compare two things and do something if they are equal, like checking if the current time is equal to the alarm you have set.
< (less than): value < value; > (greater than): value > value;
The less than and greater than comparators return 'true' when their comparison is true. If the values are equal, these comparators will return 'false'.
<= (less than or equal to): value <= value; >= (greater than or equal to): value >= value;
The less-than-or-equal-to and greater-than-or-equal-to comparators return 'true' when the comparison is true. They also return true when the values are equal. To cover an entire range of numbers, you can combine a'greater than' and 'less-than-or-equal-to' comparators so that no matter what value you are comparing to, one statement will always return 'true'.
Boolean operators can be used to combine two comparisons to get a single result.
&& and: if (a == 0 && b == 1)
The && boolean operator combines two comparisons and returns a single result: 'true' or 'false'. 'True' is returned if both of the conditions you combined with && are true. 'False' is returned if either one or both of the conditions is false.
|| (or): if (a == 0 || b == 1)
The || (usually formed with the Shift + \ key) combines two comparisons and returns a single result: 'true' or 'false'. 'True' is returned if either of the conditions you combined with || are true or if both are true. If neither of the conditions is true, 'false' is returned.
! not: if (a != 0)
The 'not' operator can be added on to boolean variables and comparators to reverse whatever value they returned. For example, !(75 > 50) will return false.
x++ increment: x = x + 1 x-- decrement: x = x - 1
Compound operators simplify the job of adding one to a variable or subtracting one from a variable. They are simply a shortcut.