官术网_书友最值得收藏!

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.

Exercises

  1. What do you think will be the value of !!wearingSocks when the value of wearingSocks is true?
  2. What is the value of isVisible after the following code is run?
bool hidden = true;
bool isVisible = !hidden;

Solution

  1. 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.
  2. The answer to the second question is false. hidden was true, so !hidden is false. false then gets saved into the isVisible 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;
}
主站蜘蛛池模板: 闵行区| 深水埗区| 本溪| 常熟市| 孟州市| 东乡县| 陇西县| 金塔县| 耒阳市| 东安县| 遂川县| 宽城| 若尔盖县| 海林市| 锡林郭勒盟| 昆山市| 天镇县| 镇赉县| 会泽县| 修水县| 临猗县| 黄山市| 上思县| 巴里| 普宁市| 通榆县| 临海市| 拉萨市| 侯马市| 颍上县| 长顺县| 阳泉市| 闸北区| 泗洪县| 琼海市| 雷波县| 宁阳县| 宜昌市| 玉山县| 旺苍县| 巴里|