 # Boolean (Logical) operators in Arduino

In previous three posts we have seen about arithmetic, relational and bitwise operators. We have one more operators left, which we will discuss in this tutorial. So, we have total four operators.

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

## Boolean operators

These operators return true (1) or false (0) output according to operand given to it and operator used. It is can be used to check two conditions and perform any action if to or more condition are met. Let’s see how many operator we have in this category.

Suppose we are performing any operation based on any condition and we have more than one condition. In that case these operators are used mostly.

### Logical AND

This operator takes two numbers as input parameter on its right and left side of ‘&’ symbol. Logical AND operator returns ‘1’ if both operands (number) are non-zero.

``````int a = 1;		// declaring a as integer and assigning it 1
int b = 2;		// declaring b as integer and assigning it 2
int c;			// to store the result
void setup() {		// put your setup code here, to run once:
Serial.begin(9600);	// initializing the serial communication
c = a&&b;		//performing logical AND
Serial.println(c);	// serial printing the result
b=0;			// re-assigning b with 0
c = a&&b;		//performing logical AND
Serial.println(c);	// serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output on serial monitor:             1

0

As you can see that we have the output as we have expected. First of all we have declared three variable a, b and c. We have assigned 1 to a and 2 to b. ‘c’ is used to store the result of operation we will perform on a and b.

In the void setup block we have initialized the serial communication at the 9600 baud rate. Then we have performed the logical and operation on a and b and stored the result in c. Then we have serial printed the value of c. In the next line of code we have changed the value of b to 0 from 2 and repeated the same process.

This operator is mostly used to check conditions. Suppose we have more than one condition and we want to perform any operation if all our condition are satisfied. Let’s see an example.

``````int age;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Enter the age: ");

while (1)
{
if (Serial.available() > 0) {
age = Serial.parseInt();
break;
}
}
Serial.println(age);
if (age <= 12) {
Serial.println("You are child.");
}
else if (age > 12 && age < 20) {
Serial.println("You are teenager.");
}
else if (age > 19 && age < 40) {
}
else{
Serial.println("You are senior citizen");
}
}``````

Output on serial monitor:

Here we have designed a software using logical operator in Arduino serial monitor which will tell us in what age stage we are based upon our age. For this we have to enter our age on serial monitor and it will tell us our age stage. For age below 12 years it will show child, age between 12 years and 20 years it will show teenager, age between 20 and 40 it will show adult and age above 40 it will show senior citizen. Now let’s understand the code.

We have declared a integer type variable called “age” to store the age that we will give in serial monitor. In void setup block we have initialized the serial communication at the baud rate of 9600.

In void loop block we have serial printed a message asking for the enter the age. Then we have started a infinite while loop and continuously checked for any serial data. This loop will continue looping until any serial data is not received. This way the code will pause until we don’t enter the age. As soon as we enter the age it will store it to age variable and break the loop.

Now if age is less than 12 years then we serial printed “You are child”. In the second and third condition we have logical and operator to check for two condition to define an age band. In second condition we have checked if age is greater than 12 years and less than 20 years. If it is true then we have serial printed that “You are teenager”. In third condition we have checked if age is greater than 20 years and less than 40 years.

So this is how you can make decisions based upon two or more conditions using logical operators.

### Logical OR

This operator also takes two numbers as input parameter on its right and left side of ‘||’ symbol. Logical OR operator returns ‘1’ if any of both or both operands (number) are non-zero.

``````int a = 1;		// declaring a as integer and assigning it 1
int b = 2;		// declaring b as integer and assigning it 2
int c;			// to store the result
void setup() {		// put your setup code here, to run once:
Serial.begin(9600);	// initializing the serial communication
c = a||b;		//performing logical OR
Serial.println(c);	// serial printing the result
b=0;			// re-assigning b with 0
c = a||b;		//performing logical OR
Serial.println(c);	// serial printing the result
a=0;			// re-assigning a with 0
c = a||b;		//performing logical OR
Serial.println(c);	// serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output on serial monitor:             1

1

0

Here we have declared three variable a, b and c. a nd b are used as operand on which we will perform logical operation and c is used to store the result of that operation. a is initially assigned with 1 and b is initially assigned with 2.

In void setup block we have initialized the serial communication to see the outputs. Then we have performed first logical OR between a and b when a is 1 and b is 2. So answer will be 1 and that is stored in the variable c. Then we have serial printed the c. Then we have changed the value of b and a then performed logical OR operation and printed the result.

We have seen that using logical AND operator we can perform any operation if two more than two conditions are met. But using logical OR operator we can perform an operation if one or more of our conditions are met.

### Logical NOT

Logical NOT invert the logical state of its operand. Operand should be written on the right side of “!” symbol. It will turn a ‘0’ to ‘1’ and any non-zero number to ‘0’.

``````int c;			// to store the result
void setup() {		// put your setup code here, to run once:
Serial.begin(9600);	// initializing the serial communication
c = !0;			//performing logical NOT
Serial.println(c);	// serial printing the result
c = !1;			//performing logical NOT
Serial.println(c);	// serial printing the result
c = !2;			//performing logical NOT
Serial.println(c);	// serial printing the result
c = !(1&&2);			//performing logical NOT
Serial.println(c);	// serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output on serial monitor:             1

0

0

0

We will understand this operator using an example. We will create a program who will ask us for a number through serial monitor. Then it will tell us whether it is even or odd. Let’s see the screenshot of that serial monitor.

So you can see that it showing that whether number is even or odd. Now let’s see the code for this program.

``````int number;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Enter a number: ");
while (1) {
if (Serial.available() > 0) {
number = Serial.parseInt();
break;
}
}
Serial.println(number);
if (number % 2 != 0) {
Serial.println("Number is odd");
}
else {
Serial.println("Numebr is even");
}
}``````

We have first declared a integer type variable called number to store the number that we will give through serial monitor. In the setup section we have initialized the serial communication.

In the loop function we have sent a message to user to enter the number. Then we started an infinite while loop which will keep running until we don’t give any number as input.

After we have given the input then we will check the number if it is divisible by 2. For this we have divided the number and check the remainder. Modulus(%) is that operator which will give us remainder. After getting the remainder we will equate it with 0. If it is not equal to (!=) 0 then it is odd number else it is even number.

### 3 thoughts on “Boolean (Logical) operators in Arduino”

1. Having read this I believed it was rather enlightening.

I appreciate you taking the time and effort to put this short article together.

I once again find myself spending way too much time both reading and commenting.

But so what, it was still worthwhile!

2. Interesting explanation, how can you implement the OR , AND and NOT in for electrical control system. That is for switches in control system as if S1 AND S3 = 1 , SET OUTPUT 2 HIGH. If S4 OR S1 = 1 ,SET OUTPUT 3 HIGH. And so on ,some example will be welcome for application of motor drive controls.
Wish to hear from You Soon
Safoor Ramjan
From Mauritius

1. Welcome, Ramjan. You have given a very good explanation. Thank you.