Arduino logical/Boolean Operators and Relational Operator

What is the Boolean/logical and Relational/Comparison operator in Arduino, and what do these operators do in code? & how to use Boolean/logical and Comparison operators in Arduino programming are some questions that may arise while programming an Arduino.

Hello guys! Welcome back to my blog today we are going to see about Boolean/logical and Comparison/relational operators in Arduino, so let’s begin.

Comparison or Relational Operator:

A comparison operator is used in condition statements. A comparison operator is used to compare two numbers or strings. Comparison operator can also be used alone example (! A) Or (! 9) or (! value). Comparison operators are mostly used with if else conditions. Comparison operators are used to determine the difference, equality, and value availability. Comparison Expressions either return 0 when the condition is false or 1 when the condition is true. 

Relational Operators Symbol Example
Less than < 5 < 10
Greater than > 5 > 2
Less than or equal to <= 5 < =5
Greater than or equal to => 5 => 5
Equal to == 5 == 5
Not equal to ! = 5 != 6

  • Less than (<)

Less than operator is used to compare two numbers and see if the first number is less than the second number. If the first number is less than the second number then the result is true. Example (5<10) here first number is 5 and the second number is 10 so as we know 5 is less than 10 so the result will be true.

  • Greater than (>)

Greater than operator is used to compare two numbers and see if the first number is less than the second number. If the first number is Greater than the second number then the result is true. Example (5>2) here the first number is 5 and the second number is 2 so as we know 5 is Greater than 2 so the result will be true.

  • Less than or equal to (=<)

Less than or equal operator is same as less than operator but the only difference is it will compare equal to also. If the first number is less than the second number or equal to the second number then the result is true. Example (5=<5) here the first number is 5 and the second number is 5 so as we know 5 is not less than 5 but equal to 5 so the result will be true.

  • Greater than or equal to (=<)

Greater than or equal to is the same as greater than operator but the only difference is it will compare equal to also. If the first number is less Greater than the second number or equal to the second number then the result is true. Example (5 >=5) here the first number is 5 and the second number is 5 so as we know 5 is not greater than 5 but equal to 5 so the result will be true.

  • Equal to (>)

Equal to the operator is used to compare two numbers and see if the first number is equal to the second number. If the first number is equal to the second number then the result is true. Example (5==5) here the first number is 5 and the second number is 5 so as we know 5 is equal to 5 so the result will be true.

  • Not equal to (5! == 6)

Not equal to the operator is used to compare two numbers and see if the first number is not equal to the second number. If the first number is not equal to the second number then the result is true. Example (5!==6) here the first number is 5 and the second number is 6 so as we know 5 is not equal to 6 so the result will be true.

Example - 

void setup() {
 Serial.begin(115200);
}
void loop() 
{
for(int i=0;i<=10;i++){
  if(i==5){
  Serial.print("Inside Loop :");  
  }
  Serial.println(i);
  delay(500);
  }

OUTPUT - 


Here we are taking an example of a comparison operator by simple code

Using for a loop. In for loop we initialize the value of i as 0 first and later increment till value of i reaches 10 (i = 0 to i =10). Here we have used less than equal to 10. Later we increment the value of i reaches 10. Inside for loop, we have also included if condition, here we have given condition value of i become 5 then it will be printed (“Inside Loop:”) to know we are inside if condition. Here we have used the equal-to operator to know if the value of i is equal to 5 (i==5) as we see in the output.

Logical operator:

Logical operators are used with a combination of comparison or relational operators for giving Boolean results. It returns both true and false values according to the condition. We will various logical operators one by one.

  • AND operator

In AND operator both statements (statement 1) && (statement 2) should be true and if both statements (statement 1) && (statement 2) are false then the result will be false.

The symbol of AND operator is &&

Example -   (statement 1) && (statement 2)


Statement 1 (A) Statement 2 (B) Result
0 0 0
0 1 0
1 0 0
1 1 1

Example -


void setup() {
 Serial.begin(115200);
}
void loop()
{
 int AND_Result1 = (5 > 10) && (2 > 5);
 int AND_Result2 = (5 > 10) && (5 < 10);
 int AND_Result3 = (2 < 5) && (5 > 10);
 int AND_Result4 = (2 < 5) && (2 < 4);

Serial.print("AND_Result1:");
Serial.println(AND_Result1);
Serial.print("AND_Result2:");
Serial.println(AND_Result2);
Serial.print("AND_Result3:");
Serial.println(AND_Result3);
Serial.print("AND_Result4:");
Serial.println(AND_Result4);
}

Output –

 

AND_Result1:0

AND_Result2:0

AND_Result3:0

AND_Result4:1

As we know AND operator will be true when both non-zero expressions will be true here we know in result 1 number 5 is not greater than 10 and 2 is also not greater than 5 so here both conditions (5 > 10) and (2 > 5) is not satisfied so it will be 0 and 0 hence result will be 0. In result 2 number 5 is not greater than 10 and 5 is less than 10 so here one condition (5 > 10) is not satisfied and (5< 10) is satisfied so it will be 0 and 1 hence result will be 0. In result 3 number 2 is less than 5 and 5 is not greater than 10 so here one condition (2 < 5) is satisfied and (5 > 10) is not satisfied so it will be 1 and 0 hence result will be 0. In result 4 number 2 is less than 5 and 2 is less than 4 so here both condition (2 < 5) and (5 > 10) is satisfied so it will be 1 and 1 hence result will be 1.

  • OR operator

In the OR operator, either Statement1 or Statement2 or both statements should be true and if both statements are false then the result will be false.

The symbol of the OR operator is ||

Example – (statement 1) || (statement 2)

                    

Statement 1 (A) Statement 2 (B) Result
0 0 0
0 1 1
1 0 1
1 1 1

Example -


void setup() {
 Serial.begin(115200);
}
void loop()
{
 int OR_Result1 = (5 > 10) || (2 > 5);
 int OR_Result2 = (5 > 10) || (5 < 10);
 int OR_Result3 = (2 < 5) || (5 > 10);
 int OR_Result4 = (2 < 5) || (2 < 4);

Serial.print("OR_Result1:");
Serial.println(OR_Result1);
Serial.print("OR_Result2:");
Serial.println(OR_Result2);
Serial.print("OR_Result3:");
Serial.println(OR_Result3);
Serial.print("OR_Result4:");
Serial.println(OR_Result4);
}

OUTPUT –

OR_Result1:0

OR_Result2:1

OR_Result3:1

OR_Result4:1

As we know OR operator will be true when either of the expression will be true here we know in result 1 number 5 is not greater than 10 and 2 is also not greater than 5 so here both conditions (5 > 10) and (2 > 5) is not satisfied so it will be 0 and 0 hence result will be 0. In result 2 number 5 is not greater than 10 and 5 is less than 10 so here one condition (5 > 10) is not satisfied and (5< 10) is satisfied so it will be 0 and 1 hence result will be 1. In result 3 number 2 is less than 5 and 5 is not greater than 10 so here one condition (2 < 5) is satisfied and (5 > 10) is not satisfied so it will be 1 and 0 hence result will be 1. In result 4 number 2 is less than 5 and 2 is less than 4 so here both condition (2 < 5) and (5 > 10) is satisfied so it will be 1 and 1 hence result will be 1.

  • NOT operator

NOT operator inverts the statements if the statement is 1 then NOT operator will change to 0 and if the statement is 0 then NOT operator will change to 1.

Symbol of NOT operator is!

Example - !(A || B) ,  !(A && B), !A, !B

Statement 1 (A) Statement 2 (B)
0 1
1 0

Example - 

void setup() {

 Serial.begin(115200);

}

void loop()

{

 int NOT_Result1 = !(5 > 10);

 int NOT_Result2 = !(5 < 10);


Serial.print("NOT_Result1:");

Serial.println(NOT_Result1);

Serial.print("NOT_Result2:");

Serial.println(NOT_Result2);

}

Output:

NOT_Result1:1


NOT_Result2:0

As we know NOT operator inverts the results so in result 1 here 5 is not greater than 10 (5 > 10) so the result will be false but as we know NOT Operator reverses the output so the output will be 1. Also in result 2 here 5 is less than 10 so the result will be true (5 < 10) but as we know NOT Operator reverses the output so the output will be 0.


Conclusion – 

So in this blog, we learn about what Arduino logical operators and comparison operator is, and how to use logical and comparison operator in Arduino. we also have seen examples of logical and comparison operators.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu