
Using logical operators
Logical operators allow you to do more complex checks, rather than checking for a simple equality or inequality. Say, for example, the condition to gain entry into a special room requires the player to have both the red and green keycards. We want to check whether two conditions hold true at the same time. To do this type of complex logic statement checks, there are three additional constructs that we need to learn: the not (!
), and (&&
), and or (||
) operators.
The Not (!) operator
The !
operator is handy to reverse the value of a boolean
variable. Take an example of the following code:
bool wearingSocks = true; if( !wearingSocks ) // same as if( false == wearingSocks ) { cout << "Get some socks on!" << endl; } else { cout << "You already have socks" << endl; }
The if
statement here checks whether or not you are wearing socks. Then, you are issued a command to get some socks on. The !
operator reverses the value of whatever is in the boolean
variable to be the opposite value.
We use something called a truth table to show all the possible results of using the !
operator on a boolean
variable, as follows:

So, when wearingSocks
has the value true, !wearingSocks
has the value false
and vice versa.
- What do you think will be the value of
!!wearingSocks
when the value ofwearingSocks
is true? - What is the value of
isVisible
after the following code is run?
bool hidden = true; bool isVisible = !hidden;
- If
wearingSocks
is true, then!wearingSocks
is false. Therefore,!!wearingSocks
becomes true again. It's like saying I am not not hungry. Not not is a double negative, so this sentence means that I am actually hungry. - The answer to the second question is false.
hidden
was true, so!hidden
is false. false then gets saved into theisVisible
variable.
Tip
The !
operator is sometimes colloquially known as bang. The preceding bang bang operation (!!
) is a double negative and a double logical inversion. If you bang-bang a bool
variable, there is no net change to the variable. If you bang-bang an int
variable, it becomes a simple bool
variable(true
or false
). If the int
value is greater than zero, it is reduced to a simple true
. If the int
value is 0 already, it is reduced to a simple false
.
The And (&&) operator
Say, we only want to run a section of the code if two conditions are true. For example, we are only dressed if we are wearing both socks and clothes. You can use the following code to checks this:
bool wearingSocks = true; bool wearingClothes = false; if( wearingSocks && wearingClothes )// && requires BOTH to be true { cout << "You are dressed!" << endl; } else { cout << "You are not dressed yet" << endl; }
The Or (||) operator
We sometimes want to run a section of the code if either one of the variables is true
.
So, for example, say the player wins a certain bonus if he finds either a special star in the level or the time that he takes to complete the level is less than 60 seconds, in which case you can use the following code:
bool foundStar = true; float levelCompleteTime = 25.f; float maxTimeForBonus = 60.f; // || requires EITHER to be true to get in the { below if( foundStar || levelCompleteTime < maxTimeForBonus ) { cout << "Bonus awarded!" << endl; } else { cout << "No bonus." << endl; }