Recall that C++'s if
statement executes its
embedded statement if the value of the Boolean expression is not
0
. C++ has no complimentary statement, which might be called
unless, if it existed, for which the embedded statement is executed
when the value of the Boolean expression is 0.
One way to get the effect of the nonexistent unless
statement by prefacing the Boolean expression with the not
operator, !
. Thus, the following tests are equivalent:
if (temperature < 25) cout << "It is too cold!" << endl; if (!(temperature >= 25)) cout << "It is too cold!" << endl;
Another way to get the effect of the nonexistent unless
statement is via an if-else
statement with an empty
statementone that consists of a semicolon onlyin the if-true
position. Thus, the following tests are equivalent:
if (temperature < 25) cout << "It is too cold!" << endl; if (temperature >= 25) ; else cout << "It is too cold!" << endl;