Sie sind auf Seite 1von 3

Don Bosco Techncical Institute – Makati

Junior High School Department


School Year 2019 – 2020

ORDER OF OPERATIONS

By Jadrien Morales | Jherald Collado

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:

• Define what order of operation is


• Identify how to use the operations in a sketch
• Understand how the operations work

Definition of Terms

Operand - the quantity on which an operation is to be done.

Int – Integer; data type for number storage

Floating point - numbers that contain floating decimal points


Operation

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.

II. Subtraction - subtracts one number from another


using the minus sign (-)
a. Here’s an example on the right. The result of
this calculation will be 8 and will be stored in the
result variable.

III. Multiplication - done by using the multiplication operator (*).


a. Here’s an example on the right. The result of this
calculation will be 12 and will be stored in the result
variable.

IV. Division - The division operator (/) is used to perform


division in the Arduino.
a. The result of the calculation on the right is 4.
V. Remainder (Modulo Division) - The remainder operator (or modulo operator) is
used to find the remainder after the division of two numbers. The percentage sign
(%) is used as the modulo operator.
a. The result of this calculation will be the remainder of 11 divided by 4 which
is 3 (4 goes into 11 twice leaving a remainder of 3).

VI. Difference of integers and floating point


a. If the result of a division is not an integer (or whole number), but contains
a fraction part, the fraction part will be discarded if the result is stored in an
integer variable.
b. The following examples will demonstrate what happens when a result with
a fractional part is stored in an integer and then a floating point variable.

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

Arithmetic. (n.d.). Retrieved from https://www.arduino.cc/en/Reference.Arithmetic.

Smith, W. A. (n.d.). Arithmetic Operators. Retrieved from


https://startingelectronics.org/software/arduino/learn-to-program-course/04-arithmetic-
operators/.

O’Reilly | Safari. (2020). Arduino Cookbook, 2nd Edition. [online] Available at:
https://www.oreilly.com/library/view/arduino-cookbook-2nd/9781449321185/.

Das könnte Ihnen auch gefallen