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...