Logical operators
Before going any further, let's learn about a few logical operators. There are only three types of logical operators in C. They are, AND
, OR,
and NOT.
All three operators cover any logical operation. These operators help to remove complexities and shorten the code. Let's learn their symbols first:
Now we will learn about the operators in detail.
AND operator
The AND
operator means if all the conditions fulfill the condition, the condition is true.
Let's look at an example.
Suppose, if today is Monday and the weather is good, I will go to school. See, if both conditions is true, I will do something. But if any of the conditions are false, I won't go to school. In this case, we can either use a nested if or a logical operator inside the if -statement. Let's look at the code.
We first declare two variables:
String Day; String Weather; //it can be a Boolean too. Where true = good and false = not good
Now we set the values of the variables:
Day = "Monday"; Weather = "Good";
Now we declare an if-statement, where we will use an AND
operator:
if(Day == "Monday" && Weather == "Good"){ Serial.println("I will go to school . "); }
If we run the code, we will get I will go to school on the console. Do you know why? Because both conditions are true. Still don't believe it? Let's look at the output:
Let's change the value of Day
to something else and rerun the code. What do you think? Will there be any output? Yes, you are right. The Serial Monitor will be blank. Let's look at what happens if we change
the value of Day
to Sunday
:
Let's come to a conclusion. The AND
operator is used to check if all the conditions are true
. If any of the conditions are false
, the program will not execute.
Now, check the outputs for the following conditions:
OR operator
The OR
operator means if any of the conditions fulfill the condition, then the condition is true
.
Let's look at an example.
If I have a working mouse or a working keyboard, I can play a game. See, I can play the game if either of the devices I have is working. So, our if-statement has two conditions, as before, but with an OR operator, so that if any of the conditions is true, our console shows something. We need to define two variables. This time, let's use two Boolean variables:
bool mouse; bool keyboard;
Inside the setup
function, declare the variables:
mouse = 0; // This means I don't have a mouse. If I had, do you know what I would write? keyboard = 1; //This means I have a keyboard.
Let's write our if-statement:
If(mouse == 1 || keyboard == 1) { Serial.println("You can play the game! "); }
Let's look at the output:
Yes, I can play the game. Now, can you tell me, if I replaced 0
or 1
with false
and true
, respectively, would the answer be the same?
How about we change the code as follows and look at the output?
bool mouse; bool keyboard; void setup() { Serial.begin(9600); mouse = false; keyboard = true; if (mouse == true || keyboard == true) { Serial.println("You can play the game"); } } void loop() { }
The output is exactly the same as previously. Can you explain why? I guess you know the answer. Can you tell me the output of the same code if our values for the variables were as follows?
NOT operator
The NOT
operator inverts the condition. If our condition (only one) is true, our program will not be executed. If false, the program will be executed:
If a ==b is true, then !(a==b) is false.
Let's look at an example:
- A peculiar job says you need to be exactly six feet tall to be qualified for the job. If you are not, then you will automatically be disqualified. Let's write a program to check if you are qualified or not.
- Let's declare a variable first:
float height;
- Now assign it to your height:
height = 5.11;
- Now write the if-statement:
if(height!=6){ Serial.println("Sorry. You are not qualified."); }
- So, the full code and output are as follows:
float height; void setup() { Serial.begin(9600); height = 5.11; if (height != 6) { Serial.println("Sorry. You are not qualified!"); } } void loop() { }
Now, check the output for the following inputs of height:
If-else
Sometimes our condition has an exception. Say, I will go outside if the weather is good, or I will stay home otherwise. See, something will be done, but if the condition is not met, another thing will be done that is the opposite of the first statement. The basic syntax for the if-else statement is as follows:
if (condition) { statements //This will be executed if the condition is true } else { statements //This will be executed if the condition is false }
Let's look at an example:
Ron and Laura are two friends. They went to the mall and bought some pencils. If Ron's number of pencils is greater than Laura's, then Laura will give Ron half of her pencils. Otherwise (else), Ron will give Laura half of his pencils. Find the total number of pencils after all calculations, when Ron buys 130 pencils and Laura buys 146 pencils.
Let's declare two variables for the number of pencils first:
int Ron = 130; int Laura = 146; Now construct our if-else statements. if (Ron > Laura) { Serial.println("Ron wins. Ron now has "); Serial.print( Ron + Laura / 2); Serial.print(" pencils"); } else { Serial.println("Laura wins. Laura now has "); Serial.print( Laura + Ron / 2); Serial.print(" pencils"); }
Fire up the Arduino and run the following full code. You will see the following output:
int Ron = 130; int Laura = 146; void setup() { Serial.begin(9600); if (Ron > Laura) { Serial.println("Ron wins. Ron now has "); Serial.print( Ron + Laura / 2); Serial.print(" pencils"); } else { Serial.println("Laura wins. Laura now has "); Serial.print( Laura + Ron / 2); Serial.print(" pencils"); } } void loop() { }
Look at the output. The part inside the else
was executed. Why? Because the condition was not fulfilled. If the condition was fulfilled, the code inside the else would not be executed. Let's change the values of Ron and Laura to 150 and 140, respectively, and look at the output.
The output will be as follows:
Can you see the difference? This time, Ron has more pencils.
Let's do something more complicated. Say, Ron buys 170 pencils and 168 pens. Laura buys 340 pens and 106 pencils.
Write a program for the following conditions:
- Case 1: If Ron has more than 150 pencils and Laura has fewer than 120 pencils, they will share the pencils equally, but not the pens. Otherwise, Ron will own all the pencils. Find out who will have how many pens and pencils.
- Case 2: If the total number of pencils is greater than the total number of pens, Ron will buy all the pens; if not, Laura will buy all the pencils. If the first condition is true, there is another condition: if the sum of pens and pencils of one inpidual is greater than the other's, the person who has more will have to buy 100 pencils from the other. Find the number of pens and pencils Ron and Laura each have.
For the first case, we need to define a few variables, as follows:
int RPencils = 170, RPens = 168, LPencils = 106, LPens =340;
Now construct out conditions as follows:
if (RPencils > 150 && LPencils < 120) { Serial.println("Ron and Laura both have "); Serial.print((RPencils + LPencils) / 2); Serial.print(" pencils"); Serial.println("And Ron has "); Serial.print(RPens); Serial.print(" pens, "); Serial.println(" And Laura has "); Serial.print(LPens); Serial.print(" pens, "); } else { Serial.print("Ron has "); Serial.print(RPencils + LPencils); Serial.print(" pencils. And"); Serial.print(RPens); Serial.print(" pens. "); Serial.print("Laura has no pencil but"); Serial.print(RPens); Serial.print(" pens"); }
Can you say which part of the code will be executed? Yes, the if part. Because both the conditions inside the if part are true
. So, the output will be as follows:
Change the values of LPencils
to 130 and look at the result. The output will be as follows:
Can you tell why the else part executed? Yes, because one of the conditions inside the if
part was not true.
Let's move to the second case. For this case, our variables will remain the same as the first case. We can see that we will need an if-else
statement. But the if-statement will contain another if-statement, right? Let's write the conditions:
if ((RPencils + LPencils) > (RPens + LPens)) { Serial.print("Ron buys Laura's pen and he has "); Serial.print(RPens + LPens); Serial.println(" pens"); if ((RPens + RPencils) > (LPens + LPencils)) { Serial.println("Ron will buy Laura 100 pencils"); } else { Serial.println("Laura will buy Ron 100 pencils"); } } else { Serial.print("Laura buys Ron's pencils and she has "); Serial.print(RPencils + LPencils); Serial.print(" pencils"); }
Can you tell the output from the code? I hope you can. Let's look at the output:
Yes, the else part was executed! Can you set the variables so that the if part executes?
Switch-case
If we have multiple conditions, we sometimes use switch-case
for the simplicity of the logic. The basic structure of switch-case
is as follows:
switch (expression) { case constant-expression : statement; break; case constant-expression : statement; break; //more cases default : statement; }
Here, by expression, we mean the variable itself. constant-expression denotes the value of the condition. We have to write break after every case to avoid the execution of the next case statements. If none of the cases match the expression, the default case executes. The default case does not need to have a break. Let's look at an example.
Mary bakes cakes. She codes her cakes as follows, for a quick response to her customers:
- 101 is Layer cake
- 102 is Lamington cake
- 103 is Lady Baltimore cake
- 104 is Apple cake
- 105 is Coconut cake
- 106 is Frog cake
- 107 is Madeira cake
- 108 is Rum cake
- 109 is Strawberry cake
- 110 is Welsh cake
The customer types the code number and Mary takes the order. Write a program that will contain the customer's order. If the code doesn't match any of the codes, the customer will get an error message.
Declare an integer variable, customer, and assign 105
to it:
int customer = 105;
Now write the switch-case
for our problem. The full code is as follows:
int customer = 105 ; void setup() { Serial.begin(9600); switch (customer) { //We will check customer's value case 101: Serial.print("Layer cake"); break; case 102: Serial.print("Lamington"); break; case 103: Serial.print("Lady Baltimore cake"); break; case 104: Serial.print("Apple cake"); break; case 105: Serial.print("Coconut cake"); break; case 106: Serial.print("Frog cake"); break; case 107: Serial.print("Madeira cake"); break; case 108: Serial.print("Rum cake"); break; case 109: Serial.print("Strawberry cake"); break; case 110: Serial.print("Welsh cake"); break; default: //If no case is true Serial.print("Error! Please enter a correct code! "); } } void loop() { }
The output of the code is as follows:
Now change the values to customer, as follows, and check your outputs:
Let's do another program. We will find out the largest of three integers.
Let's declare three variables and assign integer numbers to them, as follows:
int a = 10; int b = 20; int c = 30;
We will use if, if-else
, and nested if to find the largest number.
With an if-statement
, we can use the following code. Don't worry, I'll explain it soon:
if (a >= b && a >= c) { Serial.print(a); Serial.print(" is the largest number"); } if (b >= a && b >= c) { Serial.print(b); Serial.print(" is the largest number"); } if (c >= a && c >= b) { Serial.print(c); Serial.print(" is the largest number"); }
The logic is simple. If a is greater than both b and c, then a is the largest. If b is greater than both a and c, then b is the largest. If c is greater than both a and b, then c is the largest. Finally, we found the largest number, which is 30. Let's look at the output:
Let's see whether if-else
gives the same result. The main code should be as follows:
if (a >= b) { if (a >= c) { Serial.print(a); Serial.print(" is the largest"); } else { Serial.print(c); Serial.println(" is the largest."); } } else { if (b >= c) { Serial.print(b); Serial.println(" is the largest."); } else { Serial.print(c); Serial.println(" is the largest."); } }
We used one main if-else
statement, and inside the if, we added the condition, if a is greater than or equal to b. If the condition is fulfilled, there is another if-else
statement, where the if has the condition, if a is greater than or equal to c. If this is fulfilled, then we would say a is the largest; else, c is the largest. The else part of the first if executes when the first if is not true. Inside the else, we have another if-else
, which will be executed if a is not greater than or equal to b. Inside the else, the first if has the condition: if b is greater than or equal to c, then b is the largest; else, c is the largest. Let's look at the output to check if our code is right:
Yes, our output is the same.
Now we will use a slightly different thing, called else-if
, to find the largest number.
The main part of the code will be as follows:
if ( a >= b && a >= c) { Serial.print(a); Serial.print(" is the largest"); } else { if (b >= a && b >= c) { Serial.print(b); Serial.print(" is the largest"); } else { Serial.print(c); Serial.print(" is the largest"); } }
We used an if-else
statement. Inside the else part, we used another if-else
statement. If the conditions inside the outer if
do not fulfill, the else part will be executed. Inside the outer if
, we said, if a is greater than or equal to b and a is greater than or equal to c, then a is the largest number. Inside the else part, we have another if that has the condition, if b is greater than or equal to a and b is greater than or equal to c. If the condition is true, then b is the largest; else c is the largest. Let's look at the output:
Yes! The output is the same. Can you tell which of the algorithms is easy to use and why? Now let's do thane exercise to sharpen your learning.