Operations on Variables
An operation is an action like addition, subtraction, multiplication or comparison that involves one or more variables. Operations in Arduino sketches follow the syntax rules of the C language.
Table of contents
Mathematical operations
Some Arduino statements look like mathematical formulas. These formulas use the basic operations of addition, subtraction, multiplication and division.
Add, subtract, multiply and divide
Here are two examples of operations
a + b
2.0 * sqrt(c)
These operations have the common sense interpretation of performing mathematical calculations. Assuming that a
, b
and c
are variables that store numerical values (an assumption that may or may not be true!), the result of those operations is another numerical value.
Note that *
is the multiplication operator and sqrt
is a builtin function that computes the square root of a number. Therefore, 2.0 * sqrt(c)
performs the mathematical operation, “two times the square root of c
”.
Operator Precedence
The order of operations matter. The C language has rules to specify which operators have priority in an expression. Rather than rely on those rules, it is much better to use parenthesis to group operations. We will demonstrate with some examples.
Consider the following code snippet. Will d
and e
be equal for all values of a
, b
and c
?
int a, b, c, d, e;
a = ...; // Assign some values
b = ...;
c = ...;
d = a + b*c
e = (a + b)*c
The answer is “no”. d
and e
will not be equal for all values of a
, b
and c
.
The basic rule for evaluating these expressions is to apply operators from left to right except when higher order operators appear on the right. The multiplication operator has higher precedence than the addition operator. Therefore, in the expression a + b*c
, the multiplication b*c
is performed first before the result is added to a
. If the goal is to add a
and b
before multiplying by c
, the formula for e
should be used.
When in doubt, use parenthesis. When the code is converted to machine language by the compiler, there is no cost in performance or accuracy when the code includes extra parentheses.
There is no power operator in C
Programmers familiar with Excel, MATLAB and FORTRAN may expect C to have a power operator but it doesn’t. If you want compute the square of a numerical value in C you have two choices:
x*x
pow(x,2)
The use of x*x
to compute “x-squared” extends to other integer powers. However, if you want to raise a number to a fraction power you need to use the pow
function.
Operations with mixed types
Logical operations
Logical operations involve comparisons and combinations of logical expressions
Basic
Here is a basic comparison operation
x < y
The result of a comparison operation is a logical value, either True
or False
. For example, if 5
is stored in x
and 3
is stored in y
, then the result of x < y
is the value, False
.
Unary
Here is a so-called unary operation
!z
The !
operator could be spoken as “not”, as in “not z”. This unary operator changes the meaning of z
to its opposite, which makes the most sense if z
is storing a logical value. To be specific, if the value in z
is True
then the result of !z
is False
.