Arduino OperatorsThe operators are widely used in Arduino programming from basics to advanced levels. It plays a crucial role in every programming concept like C, C++, Java, etc. The operators are used to solve logical and mathematical problems. For example, to calculate the temperature given by the sensor based on some analog voltage. The types of Operators classified in Arduino are:
Arithmetic OperatorsThere are six basic operators responsible for performing mathematical operations in Arduino, which are listed below:
The Assignment operator in Arduino is used to set the variable's value. It is quite different from the equal symbol (=) normally used in mathematics.
The addition operator is used for the addition of two numbers. For example, P + Q.
Subtraction is used to subtract one value from the another. For example, P  Q.
The multiplication is used to multiply two numbers. For example, P * Q.
The division is used to determine the result of one number divided with another. For example, P/Q.
The Modulo operator is used to calculate the remainder after the division of one number by another number. Most of the operators are similar to the usual operator used in mathematics. Let's understand the operators with the help of two examples. Example 1: Consider the below code. In the above code, we have assigned the result of the addition of two numbers to b before printing it to the console. For output, click on the Upload and Serial Monitor button present on the toolbar. Output: 7 Example 2: Consider the below code: Here, d= d +3 is not operated as a usual mathematical operation. It is the assignment operator where right of the function is evaluated first and is assigned to the left of the equal sign. Let's consider the below image for better understanding. Output: 3 6 Similarly, we can perform multiplication, modulo, and division. The int variable will store the integer values. For example, 20/3 = 6. If we want decimal values to be printed, we need to use the float instead of int. For example, Consider the below code: Output: 6.66 Order of mathematical operationsLet's understand the order of operations considered by the Arduino while performing calculation:
If there are multiple operations next to each other, they will be computed from left to right. Let's understand with an example. Consider the below code: Output: 6 Let's understand how the above output occurred. Consider the below image: Compound OperatorsThe compound operators perform two or more calculations at once. The result of the right operand is assigned to the left operand, as already discussed above. The same condition will apply to all the compound operators, which are listed below: Let's consider a variable b.
Here, b = b + 1. It is called the increment operator.
For example, b + = 4. It means, b = b+ 4.
Here, b = b  1. It is called as the decrement operator.
For example, b  = 3. It means, b = b  3.
For example, b * = 6. It means, b = b * 6.
For example, b / = 5. It means, b = b / 5.
For example, b % = 2. It means, b = b % 2. Now, let's use the above operators with two variables, b and c.
We can specify any variable instead of b and c. Boolean OperatorsThe Boolean Operators are NOT ( ! ), Logical AND ( & & ), and Logical OR (   ). Let's discuss the above operators in detail.
The result of the condition is true if both the operands in the condition are true. Consider the below example: Above statement is true if both conditions are true. If any of the conditions is false, the statement will be false.
The result of the condition is true, if either of the variables in the condition is true. Consider the below example. The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.
It is used to reverse the logical state of the operand. For example, a ! = 2. The NOT operator returns the value 1 or TRUE when the specified operand is FALSE. It also reverses the value of the specified expression. Comparison OperatorsThe comparison operators are used to compare the value of one variable with the other. The comparison operators are listed below:
The less than operator checks that the value of the left operand is less than the right operand. The statement is true if the condition is satisfied. Consider the below code. Output: 3 In the above code, if any of the two statement is correct, the corresponding value of the variable will be printed. Here, only first condition is correct. Hence, the value of b will be printed.
The less than operator checks that the value of the left side of a statement is greater than the right side. The statement is true if the condition is satisfied. For example, a > b. If a is greater than b, the condition is true, else false.
It checks the value of two operands. If the values are equal, the condition is satisfied. For example, a = = b. The above statement is used to check if the value of a is equal to b or not.
It checks the value of two specified variables. If the values are not equal, the condition will be correct and satisfied. For example, a ! = b.
The less or equal than operator checks that the value of left side of a statement is less or equal to the value on right side. The statement is true if either of the condition is satisfied. For example, a < = b It checks the value of a is less or equal than b.
The greater or equal than operator checks that the value of the left side of a statement is greater or equal to the value on the right side of that statement. The statement is true if the condition is satisfied. For example, a > = b It checks the value of a is greater or equal than b. If either of the condition satisfies, the statement is true. Bitwise OperatorsThe Bitwise operators operate at the binary level. These operators are quite easy to use. There are various bitwise operators. Some of the popular operators are listed below:
The bitwise NOT operator acts as a complement for reversing the bits. For example, if b = 1, the NOT operator will make the value of b = 0. Let's understand with another example.
The output is 0 if both the inputs are same, and it is 1 if the two input bits are different. For example, For example,
The output is 0 if both of the inputs in the OR operation are 0. Otherwise, the output is 1. The two input patterns are of 4 bits. For example,
The output is 1 if both the inputs in the AND operation are 1. Otherwise, the output is 0. The two input patterns are of 4 bits. For example,
The left operator is shifted by the number of bits defined by the right operator.
The right operator is shifted by the number of bits defined by the left operator.
Next TopicArduino array
