Boolean Operators

The == operator tests whether two values are exactly the same. If you test two integers then the test is obvious; for example, if x is 2 and y is 3, then x == y is obviously false. However, two real numbers may not be the same even when you think so:

    double x = 1.000001 * 1000000000000; 
double y = 1000001000000;
if (x == y) std::cout << "numbers are the same";

The double type is a floating-point type held in 8 bytes, but this is not enough for the precision being used here; the value stored in the x variable is 1000000999999.9999 (to four decimal places).

The != operator tests if two values are not true. The operators > and <, test two values to see if the left-hand operand is greater than, or less than, the right-hand operand, the >= operator tests if the left-hand operand is greater than or equal to the right-hand operand, and the <= operator tests if the left-hand operand is less than or equal to the right-hand operand. These operators can be used in the if statement similar to how == is used in the preceding example. The expressions using the operators return a value of type bool and so you can use them to assign values to Boolean variables:

    int x = 10; 
int y = 11;
bool b = (x > y);
if (b) std::cout << "numbers same";
else std::cout << "numbers not same";

The assignment operator (=) has a higher precedence than the greater than (>=) operator, but we have used the parentheses to make it explicit that the value is tested before being used to assign the variable. You can use the ! operator to negate a logical value. So, using the value of b obtained previously, you can write the following:

    if (!b) std::cout << "numbers not same"; 
else std::cout << "numbers same";

You can combine two logical expressions using the && (AND) and || (OR) operators. An expression with the && operator is true only if both operands are true, whereas an expression with the || operator is true if either, or both, operands are true:

    int x = 10, y = 10, z = 9; 
if ((x == y) || (y < z))
std::cout << "one or both are true";

This code involves three tests; the first tests if the x and y variables have the same value, the second tests if the variable y is less than z, and then there is a test to see if either or both of the first two tests are true.

In a || expression such as this, where the first operand (x==y) is true, the total logical expression will be true regardless of the value of the right operand (here, y < z). So there is no point in testing the second expression. Correspondingly, in an && expression, if the first operand is false then the entire expression must be false, and so the right-hand part of the expression need not be tested.

The compiler will provide code to perform this short-circuiting for you:

    if ((x != 0) && (0.5 > 1/x))  
{
// reciprocal is less than 0.5
}

This code tests to see if the reciprocal of x is less than 0.5 (or, conversely, that x is greater than 2). If the x variable has value 0 then the test 1/x is an error but, in this case, the expression will never be executed because the left operand to && is false.