C++: Logical Operators
So now we know how to compare two values by formulating various conditions in if, else-if or else statements.
But what do we do if we want one condition and another to be true? How about if we want one condition or another to be true? What about if we want a condition to not be true?
Well luckily for us, there are these things called "logical operators" that we can use to accomplish solutions to all these questions! They basically (well, "and" and "or") let us have more than one condition in an if statement.
So let’s go through the three main logical operators (the very same ones we addressed in the questions!).
And
The "and" logical operator is used to check if one condition and another is true.Both conditions must be true for the code inside in the if statement to be executed.
It’s represented by two ampersand (&) signs next to each other (&&), and we can literally use it between two conditions in an if statement to check if the first and the second are true. For example:
int Variable = 5;
if(Variable < 5 && Variable > 0)
{
cout << "The variable is less than 5 and greater than 0!" << endl;
}
In the above example, it’s obviously being used to check if "Variable" is in-between 0 and 5 – but you can use it for any conditions you like.
Or
The "or" logical operator is very much like the "and" logical operator, however instead of checking if both conditions are true, it checks if one condition or the other is true (and if both are true, then that’s also fine).It is represented by two "pipe symbols" (|) next to each other (||) and is used exactly like the "and" operator. For example:
int Variable = 5;
if(Variable < 5 || Variable > 0)
{
cout << "The variable is less than 5 or greater than 0!" << endl;
}
So in the above example, the code inside the if statement will be executed if "Variable" is below five or above 0. It’s a bit of a stupid example since any integer will make the statement true (anything below 0 is less than 5, anything above 0 is greater than 0 and anything in-between 0-5 is less than 5 and greater than 0).
Not
The "not" logical operator is a little bit different to the last two. Instead of putting multiple conditions in an if statement, the "not" operator can check if a condition is not true. So for example if we had the condition "5==5" and then applied the "not" operator (we’ll learn how to do this in a minute!), the result would be "false".The "not" operator is indicated simply by an exclamation mark (!) before the condition (The not equal to operator being "!=" doesn’t seem so stupid now, does it?). For example:
int Variable = 5;
if(!(Variable < 5 || Variable > 0))
{
cout << "The variable is less than 5 or greater than 0!" << endl;
}
In the above case, we are inverting the condition of "Variable < 5 || Variable > 0" (which we talked about in the "or" section of this tutorial). Notice that I put this in brackets to be sure that the "not" applies to the whole of "Variable < 5 || Variable > 0" and not just to "Variable < 5". So instead of any integer value making the result "true", every integer value now makes the result "false" – hence the code inside the if statement will never be executed.
The not operator also has some uses outside of if statements, take this extremely simply example of setting a variable to "not true":
bool Boolean = !true;
It’s fairly obvious that "Boolean" is now set to "not true", which is "false".
Back to C++

