Let’s examine the && (AND) and
|| (OR) operators in more detail to figure out how they work.
In computer programming, the definition is a bit more strict than the way
those words are used in English. Consider these statements
about the following diagram:
| Part A | Part B | A && B |
|---|---|---|
| The left shape is a circle. [TRUE] | The right shape is a square. [TRUE] | The left shape is a circle AND the right shape is a square. [TRUE] |
| The left shape is a circle. [TRUE] | The right shape is a triangle. [FALSE] | The left shape is a circle AND the right shape is a triangle also. [FALSE] |
| The left shape is a diamond. [FALSE] | The right shape is a square. [TRUE] | The left shape is a diamond AND the right shape is a square also. [FALSE] |
| The left shape is a trapezoid. [FALSE] | The right shape is a star. [FALSE] | The left shape is a trapezoid AND the right shape is a star also. [FALSE] |
This shows that a compound condition with &&
is true only when both parts of the condition are true. If even
one part is false, the whole combination is false.
Now let’s look at the same diagram with ||.
| Part A | Part B | A || B |
|---|---|---|
| The left shape is a circle. [TRUE] | The right shape is a square. [TRUE] | Either the left shape is a circle OR the right shape is a square. [TRUE] |
| The left shape is a circle. [TRUE] | The right shape is a triangle. [FALSE] | Either the left shape is a circle OR the right shape is a triangle. [TRUE] |
| The left shape is a diamond. [FALSE] | The right shape is a square. [TRUE] | Either the left shape is a diamond OR the right shape is a square. [TRUE] |
| The left shape is a trapezoid. [FALSE] | The right shape is a star. [FALSE] | Either the left shape is a trapezoid OR the right shape is a star. [FALSE] |
This shows that a compound condition with ||
is true if either part of the condition is true. If even
one part is true, the whole combination is true. The only way an
|| compound turns out false is when everything is false.
When you have three or more conditions joined with &&, then
the compound is true only when all the parts are true. When you have
three or more conditions joined with ||, the compound is true
when any of the parts is true.
Let’s say I tell you that Cathy owns both a cat and a dog. You want to find out if that’s true or not, so you ask Cathy, “Do you own a cat?”
If Cathy says “no,” you don’t even have to ask her about the dog. You already know that I wasn’t correct. When the first part of an and condition is false, the whole thing is false, no matter whether the second part is true or not. Why waste time asking the second question?
Similarly, let’s say I tell you that Cathy owns either a dog or a horse, and you want to find out if that’s true or not. You start by asking Cathy, “Do you own a dog?”
If Cathy says “yes,” you don’t have to go any further. As soon as the first part of an or condition is true, the whole thing is true, no matter whether the second part is true or not.
In a similar way, C++ stops evaluating a condition as soon as it knows
whether the answer will be true or false. Let’s say I have a
variable called nPlayers (number of players) and a
totalScore and I want to test if the average score is greater
than 75. Because I can’t divide by zero, I have to test that the
number of players is greater than zero first:
if (nPlayers > 0 && totalScore / nPlayers > 75)
{
cout << "Good game, people!" << endl;
}
else
{
cout << "Better luck next time!" << endl;
}
This will work exactly as we want. If nPlayers is greater
than zero, the program must test the second half to see if the entire
condition is true or false,
so the program will do the division. If nPlayers is equal
to zero, it’s not greater than zero, so the first part of the condition
comes back false. That means the whole condition is false, so C++ does not
do the division.
When you have a compound condition, you must write out all
its parts completely. If you want to test if score is
greater than 100 and less than 200, you must write the
condition like this:
score > 100 && score < 200
You must never write it in either of the following ways. The first one will generate a syntax error, and the second way will always come back as true.
score > 100 && < 200 100 < score < 200
Similarly, to test if a number n is equal to 4 or 5, you
must write it this way:
n == 4 || n == 5
And never this way, which doesn’t give you a syntax error, but always returns true.
n == 4 || 5