“Pure logical thinking cannot yield us any knowledge of the empirical world. All knowledge of reality starts from experience and ends in it” Albert Einstein
Before reading this section you should already be familiar with operators in general and comparison-operators.
In real world, there are times that we want to do a task but it depends on multiple conditions for us to be able to do the task.
Let’s say you want to go for a walk, but there are multiple conditions that should be true for you to be able to go out. Two of these conditions would be, is it late night? Is outside freezing?
Well if your answers to these questions are true, then you’ll probably pass the walk.
In C++ programming also there are times that we want to run a task (code instructions) but it depends on multiple conditions to be true.
Also in comparison-operators section we learned how to implement one condition in if-statement but how can we run multiple conditions?
This is where we can use logical-operators.
Just like the way that and and or connects two sentences together, with the help of logical-operators, we can connect the result of multiple conditions together as well.
Here’s an introductory list of the Logical-operators:
| Operator | Symbol |
|---|---|
| AND Operator | && |
| OR Operator | || |
| Reverse the Result (Not) Operator | ! |
Two ampersands & that come after each other, create the AND operator. With the help of this operator, we can involve two or more conditions in a statement like if-statement and ONLY if the result of all conditions were true, the body of the statement (in this case if-statement) will run.
Example:
#include <iostream>
using namespace std;
int main(){
int height = 6;
int age = 17;
if (height >=5 && age == 18){
cout<<"This person can join the military"<<endl;
}else{
cout<<"This person can't join the military"<<endl;
}
return 0;
}
Output:
This person can’t join the military
As you can see, in the parentheses of the if-statement we involved two conditions. The first one was to check if the height variable is more than 5 inches and the second condition was to check if the age variable’s value is more than 18. But because the second condition is not true, the end result of these two conditions will be false. So the body of the if-statement is skipped and the body of the else-statement will run instead.
OR `||` Operator:
Just like AND operator, the use of OR || operator is to involve more than one condition. The difference however is that, when using OR || operator, we’re just looking for one condition to result true.
This means if we have 3 or more conditions, and all of them minus one, result false, the end result however is true just because that single true result.
Example:
#include <iostream>
using namespace std;
int main(){
int age = 25;
if (age > 18 || age <30){
cout<<"The person can join the military"<<endl;
}else{
cout<<"The person can't join the military"<<endl;
}
return 0;
}
Output:
The person can join the military
In the example above, any of the conditions within the if-statement result true, the final result is true as well. So in this example, the first condition returned true and that’s all needed to turn the final result to true as well(although the second one is true as well but that doesn’t matter anymore). So the body of the if-statement ran.
Reverse the Result (AKA Not) `!` Operator:
This operator is used to reverse the result of a condition. It sits on the left side of a condition and if for example the condition results true, it will become false and vice versa.
Example:
#include <iostream>
using namespace std;
int main(){
int age = 25;
if (!(age > 18) ){
cout<<"The age is higher than 18 "<<endl;
}else{
cout<<"The person's age is not above 18"<<endl;
}
return 0;
}
Output:
The person’s age is not above 18
In the example above, even though the number assigned to the age variable is higher than 18, the condition of the if statement results false and this is because we reversed the result by adding the ! operator on the left side of the condition. So the body of the if-statement is skipped and the body of the else statement ran instead.