relational or comparison operator

Relational or comparison operators in Arduino

As we know that operators are very important for any programming language. It performs any mathematical or logical operations. We have seen the arithmetic operator in previous post. In this tutorial we will see about relational or comparison operators. Arduino have these kinds of operators in Arduino.

  1. Arithmetic operators
  2. Relational (comparison) operators
  3. Bitwise operators
  4. Boolean (Logical) operator

Arduino buying links

Amazon link for IndiaAmazon link for other countries

Relational or comparison operators

These operators are used to check the relation between two operands like whether they are equal to each other or not etc.

Operator nameOperator symbol
Equal to==
Not equal to!=
Less than
Greater than
Less than or equal to<=
Greater than or equal to>=

Equal to operator

This operator is used to check whether two operands (variable) are same to each other or not. If both operands are same then it will return 1 else it will return 0. Let’s see an example.

int a=10;		//declaring variable a and assigning it 10 
int b=10;		//declaring variable b and assigning it 10 
bool c;			// declaring variable c to store check result
void setup() {  	// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
c = a==b;		// checking whether a is equal b or not and storing result in c
Serial.println(c);	// serial printing the result
b=20;			// assigning b with 20
c = a==b;		// checking whether a is equal b or not and storing result in c
Serial.println(c);	//serial printing the result 
}
void loop() {
  // put your main code here, to run repeatedly:
}

Output on serial monitor:              1

                                                            0

In this code we have assigned two variable ‘a’ and ‘b’ and assigned with same number 10. Then we declared another Boolean type variable (any variable can be chosen) ‘c’ to store the check result. In the void setup block we have first compared the ‘a’ and ‘b’ with equal to operator and stored the result in the ‘c’ then serial printed it. Then in the next line we have changed the value of ‘b’ from 10 to 20. Now the value of ‘a’ is 10 and ‘b’ is 20, so they are not same.

Not equal to operator

This operator does exact reverse of equal to operator. If both operands are same then it will return 0 else it will return 1. Let’s see an example.

int a=10;		//declaring variable a and assigning it 10 
int b=10;		//declaring variable b and assigning it 10 
bool c;			// declaring variable c to store check result
void setup() {  	// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
c = a!=b;		// checking whether a is equal b or not and storing result in c
Serial.println(c);	// serial printing the result
b=20;			// assigning b with 20
c = a!=b;		// checking whether a is equal b or not and storing result in c
Serial.println(c);	//serial printing the result 
}
void loop() {
  // put your main code here, to run repeatedly:
}

Output on serial monitor:              0

                                                            1

In this code we have assigned two variable ‘a’ and ‘b’ and assigned with same number 10. Then we declared another Boolean type variable (any variable can be chosen) ‘c’ to store the check result as we had done in previous code. In the void setup block we have first compared the ‘a’ and ‘b’ with not equal to operator and stored the result in the ‘c’ then serial printed it. Then in the next line we have changed the value of ‘b’ from 10 to 20. Now the value of ‘a’ is 10 and ‘b’ is 20, so they are not same. So not equal to operator will return ‘1’.

Note: – We can do these comparisons with any data types.

Less than operator

This operator tells that whether a number is less than another number or not. If the number is less than another number then it will return 1 else it will return 0. Number which is being compared should be on left side of “<” symbol and reference number should be on right side.

int a=15;		//declaring variable a and assigning it 10 
int b=15;		//declaring variable b and assigning it 10 
bool c;			// declaring variable c to store check result
void setup() {  	// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
c = a<b;		// checking whether a is less than b or not and storing result in c
Serial.println(c);	// serial printing the result
b=20;			// assigning b with 20
c = a<b;		// checking whether a is less than b or not and storing result in c
Serial.println(c);	//serial printing the result 
}
void loop() {
  // put your main code here, to run repeatedly:
}

Output on serial monitor:              0

                                                            1

In first condition ‘a’ and ‘b’ both are same so less than operator will return 0. Then in the next line we have changed the value of ‘b’ from 15 to 20. Now the value of ‘a’ is 15 and ‘b’ is 20, so now ‘a’ is less than ‘b’ so now less than operator will return ‘1’.

Greater than operator

This operator does exact reverse of less than operator. As its name suggests if number which is on left side of “>” is greater than number which is on right side then it will return 1 else it will return 0. Let’s see an example.

int a=20;		//declaring variable a and assigning it 10 
int b=20;		//declaring variable b and assigning it 10 
bool c;			// declaring variable c to store check result
void setup() {  	// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
c = a>b;		// checking whether a is greater than b or not and storing result in c
Serial.println(c);	// serial printing the result
b=10;			// assigning b with 20
c = a>b;		// checking whether a is greater than b or not and storing result in c
Serial.println(c);	//serial printing the result 
}
void loop() {
  // put your main code here, to run repeatedly:
}

Output on serial monitor:              0

                                                            1

In the first condition both variable is 20, no one is greater so greater than operator will return 0. Then we have changed the value of ‘b’ from 20 to 10. Now ‘a’ is greater than ‘b’ so it will return 1.

Less than or equal to and greater than or equal to operators

These operators are same as to less than and greater than operators. Difference is that they return 1 even both numbers are same. Let’s see the code.

int a=10;		//declaring variable a and assigning it 10 
int b=10;		//declaring variable b and assigning it 10 
bool c;			// declaring variable c to store check result
bool d;			// declaring variable c to store check result
void setup() {  		// put your setup code here, to run once:
Serial.begin(9600); 	//initializing the serial communication
c = a<=b;		// checking whether a is less than or equal b or not and storing result in c
d = a>=b;		// checking whether a is greater than or equal b or not and storing result in c
Serial.println(c);		// serial printing the result
Serial.println(d);	// serial printing the result
b=8;			// assigning b with 8
c = a<=b;	// checking whether a is less than or equal b or not and storing result in c
d = a>=b;		// checking whether a is greater than or equal b or not and storing result in c
Serial.println(c);		//serial printing the result 
Serial.println(d);	// serial printing the result
c = b<=a;		// checking whether b is less than or equal a or not and storing result in c
d = b>=a;		// checking whether b is greater than or equal a or not and storing result in c
Serial.println(c);		//serial printing the result 
Serial.println(d);	// serial printing the result
}
void loop() {
  // put your main code here, to run repeatedly:
}

Output on serial monitor:              1

                                                            1

                                                            0

                                                            1

                                                            1

                                                            0

In the first two conditions operators will return 1 because both numbers are same. Then we changed the ‘b’ from 10 to 8. Now in the second two conditions first condition which is less than or equal to condition will be false because ‘a’ is neither less than ‘b’ nor equal to ‘b’. Second condition will be true because greater than ‘b’. In the third two conditions we have swapped the positions of operands. Now ‘a’ is reference so output of third both condition will be reverse of second both condition.

An application software based on the comparison operator

We will create a software which will tell us the who is taller or shorter among two person based on their height. We will enter name and height of two person in serial monitor. Then it will show that who is taller and how much taller in serial monitor. Let’s see the how output will be shown in serial monitor.

As you can see that serial monitor output is showing the result as we described previously. It is asking for the both persons name and height and then it is showing who is taller. Now let’s see the code for this application.

String person1_name = "Person1";
int person1_height = 162;
String person2_name = "Person2";
int person2_height = 163;
void setup() {
  Serial.begin(9600);
}

void loop() {
  ///////////////////taking name and height of two person//////////
  Serial.print("Enter the name of first person: ");
  Serial.read();
  while (1)
  {
    if (Serial.available() > 0)
    {
      person1_name = Serial.readString();
      break;
    }
  }
  Serial.print(person1_name);
  Serial.print("Enter the height of first person(in cms): ");
  Serial.read();
  while (1)
  {
    if (Serial.available() > 0)
    {
      person1_height = Serial.parseInt();
      break;
    }
  }
  Serial.println(person1_height);
  Serial.print("Enter the name of second person: ");
  Serial.read();
  while (1)
  {
    if (Serial.available() > 0)
    {
      person2_name = Serial.readString();
      break;
    }
  }
  Serial.print(person2_name);
  Serial.print("Enter the height of second person(in cms): ");
  Serial.read();
  while (1)
  {
    if (Serial.available() > 0)
    {
      person2_height = Serial.parseInt();
      break;
    }
  }
  Serial.println(person2_height);
  /////////////////comparing the height of both person///////
  if (person1_height > person2_height)
  {
    Serial.print("Taller person: ");
    Serial.print(person1_name);
    Serial.print("Taller by height: ");
    Serial.print(person1_height - person2_height);
    Serial.println("cm(s)");
  }
  else if (person1_height < person2_height)
  {
    Serial.print("Taller person: ");
    Serial.print(person2_name);
    Serial.print("Taller by height: ");
    Serial.print(person2_height - person1_height);
    Serial.println("cm(s)");
  }
  else
    Serial.println("Height of both person are same");
}

We have first declared all the variable to store the name and height of both person. We have used the string data types to store the name and integer data types to store the height in centimeters. Then in void setup section we have initialized the serial communication with 9600 baud rate.

In the void loop section we have started a infinite while loop to take the name of first person. We have use infinite loop so that code can jump back to check if first person’s name is registered. This way Arduino will not run the next section of code until first person’s name is registered. We have repeated this process to take the height of first person, name and height of the second person as input.

We have used the readString function to read the name of both person and parseInt function to take height of both person from serial buffer. After registering the name and height of both person we have compared the height and printed the result based on the three condition that occur.

First condition can be that first person is taller, second condition can be that second person is taller and third condition can be that height of both person is same.

1 thought on “Relational or comparison operators in Arduino”

Leave a Comment

Your email address will not be published. Required fields are marked *