By Stephen R. Davis

The three logical operators that can be used to create what are known as compound conditional expressions in C++ are shown in the following table.

The Logical Operators
Operator Meaning
&& AND; true if the left- and right-hand expressions are true;
otherwise false
|| OR; true if either the left- or right-hand expressions is true;
otherwise false
! NOT; true if the expression on the right is false; otherwise

The programmer is asking two or more questions in a conditional compound expression, as in the following code snippet:

// make sure that nArgument is between 0 and 5
if (0 < nArgument && nArgument < 5)

The figure shows how three different values of nArgument are evaluated by this expression.


By the way, the snippet

if (m < nArgument && nArgument < n)

is the normal way of coding the expression “if nArgument is between m and n, exclusive”. This type of test does not include the end points — that is, this test will fail if nArgument is equal to m or n. Use the <= comparison operator if you want to include the end points.