Beruflich Dokumente
Kultur Dokumente
ORDER OF OPERATIONS
Introduction
Order of operation can be used to simple math on values in your sketch and to
control the order in which the operations are performed.
These operators return the sum, difference, product, or quotient (respectively) of the
two operands. The operation is conducted using the data type of the operands, so, for
example, 9 / 4 gives 2 since 9 and 4 are integers. This also means that the operation
can overflow if the result is larger than that which can be stored in the data type (e.g.
adding 1 to an integer with the value 32,767 gives -32,768). If the operands are of
different types, the "larger" type is used for the calculation.
Below, five arithmetic operators are described and then all put into a sketch to
demonstrate how they work on the Arduino.
Objectives:
Definition of Terms
I. Addition - To add numbers on the Arduino, we use the addition operator (+).
a. In the code below, three variables are defined. Variables a and b are each
assigned a value when they are defined.
b. The sum variable is defined, but not initialized, so
contains any random number. We will use this
variable to store the result of the addition calculation,
so the random value that sum contains will be
overwritten when we put the addition result (or sum)
into it.
c. After the statement sum = a + b; has been executed, sum will contain the value
9 ( the result of the addition of variable a and b)
d. We can also add two constant values and store the
result in a variable as shown in the right. The result
stored in sum after execution of the addition statement
will be 12 in this example.
e. Constant values and variables can also be added together
and the result stored in a variable as shown here. After
execution of the addition, sum will contain 27.
f. The remaining arithmetic operators can also operate on
constant values, variables and a mixture of both.
c. The result will be 1 because the fraction is discarded when the result is
stored in the integer variable result.
d. The same calculation, but this time defining result as a floating point
variable (float). The result now contained in the result variable is 1.25.
e. When using constant values in calculations that store the result in a
floating point variable, we use a decimal point and a zero for whole
numbers, i.e. 5.0 instead of 5 on its own.
References
O’Reilly | Safari. (2020). Arduino Cookbook, 2nd Edition. [online] Available at:
https://www.oreilly.com/library/view/arduino-cookbook-2nd/9781449321185/.