Arduino

Arduino Comparison Operators

In Arduino programming there are different types of operators. These operators play an important role in functioning of any specific task or working of a code. Most of the decisions in the code are taken on the basis of results given by these operators, in other words they have a direct influence on the output. The comparison operators are used where the conditions of the Arduino code are made for deciding the flow of the program by comparing the values.

What are comparison operators

The operators used when there is a need to compare the two or more values the comparison operators are used. There are six most commonly used types of the comparison operators.

1. Equal to operator (==)

The first comparison operator is the equal to operator; this operator only tells that whether the two values are equal, or they are not equal the sign used for this operator is double equal to operator“==”. For better understanding of equal operator an example code is given and the working of this operator is illustrated by using if else statement so if we assume a is equal to 30 and b is equal to 30 and then if we apply the equal to comparison operator (a==b) then the answer will be a is equal to b Similarly, if both a and b are not equal then output will be a is not equal to b

int a=30;

int b=30;
 
void setup() {

Serial.begin(9600);

if( a==b )

 {

     
Serial.print("a is  equal to b ");

}

else

{

  Serial.print("a is  not equal to b ");

}

}

void loop() {


}

Output

2. Not Equal to operator (!=)
To check the two values that if they are not equal the comparison operation not equal to is used. The symbol for not equal operators is an exclamation mark with equal to operator “!=”. This can be illustrated by an example: suppose the value of a has a value of 20 and b has a value of 30 the operator (a!=b) will give the output that a is not equal to b However, if the condition is false then the output will be equal to b .it is further explained with the example code using if-else statement.

int a=20;

int b=30;

void setup() {

Serial.begin(9600);

if( a!=b )

 {

     
Serial.print("a is not equal to b ");

}

else

{

  Serial.print("a is  equal to b ");

}

}

void loop() {

 
}

Output

3. Less than operator (<)

Another comparison operator is the less than operator that can be used to find out whether the value is lesser than the other value or not. The symbol used for the less than is left angle bracket “<”. For example, if a is 20 and b is 30 so by applying the less than operator (a<b) the output will be “a is less than b “ and similarly if the value for a is 30 and value for b is 20 the result will be a is not less than b.

int a=20;

int b=30;  

void setup() {

Serial.begin(9600);

if( a<b )

 {    

Serial.print("a is less than  b ");

}

else

{

  Serial.print("a is  not less than  b ");

}

}

void loop() {

 
}

Output

4. Greater than operator (>)

To compare a value with any other value to check if it is greater from the other value or not the comparison operator used for this purpose is greater than the operator. This operator is represented by a right angle bracket “>”. For instance, if a has a value of 20 and b has a value of 30 and this operator is applied (b>a), the result will be b is greater than a and if we check either a is greater than b the instruction in the else statement will be executed.

int a=20;

int b=30;  

void setup() {

Serial.begin(9600);

if( b>a )

 {    

Serial.print("b is greater than a ");

}

else

{

  Serial.print("b is not greater than a ");

}

}

void loop() {

 
}

Output

5. Greater than equal to operator (>=)

This comparison operator has two conditions one is greater than and other one is the equal to condition. So, this operator tells two things: either the value being compared is greater or equal to the other value. This operator is represented by the sign (>=). For example, if a is 20 and b is 30 the output of the operator (b>=a) will be b is greater or equal to a and if the if condition gets false then the instruction in the else statement will be executed that is b is not greater or equal to a.

int a=20;

int b=30;

void setup() {

Serial.begin(9600);

if( b>=a )

 {    

Serial.print("b is greater than or equal to a ");

}

else

{

  Serial.print("b is not greater than or equal to a ");

}

}

void loop() {

 
}

Output

6. Less than equal to operator (<=)

The last comparison operator is less than equal to operator having two operators one is less than and the other one is equal to. The symbol for this operator is (<=). Suppose the value of a is 20 and the value for b is 30 and this operator is applied to check the value of a like this (a<=b) the output will be a is less than equal to b if the if condition is true otherwise the output will be a is not less than than or equal to b.

int a=20;

int b=30;  

void setup() {

Serial.begin(9600);

if( a<=b )

 {    

Serial.print("a is less than or equal to b ");

}

else

{

  Serial.print("a is not less than or equal to b ");

}

}

void loop() {

}

Output

A brief summary of all operators is given below:

Operator Sign Description
Equal to operator == Finds if the value of two operands is equal or not: if yes condition will be true
Not Equal to operator != Finds if the values of two operands are equal or not
Less than operator < compares the two values and find which one is less than the other
Greater than operator > compares the two values and finds which one is greater than the other.
operator other or if both are equal.
Less than equal to operator <= compares the two values and finds which one is less than the other or if both are equal.

Conclusion

Whenever a code for performing a specific task is to be written. First one must create the logic on how the task will be performed. To create the logic of a program different operators come in handy and make the task easier to perform. So, in this article the comparison operators are discussed in detail. Different types of comparison operators are defined and then at the end an example of implementation of these operators in the Arduino program is given.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.