|
|
|
|
|
|
|
for computing overtime pay. If the assertion is false, the computer simply computes the regular pay. Before we examine selection control structures in C++, let's look closely at how we get the computer to make decisions. |
|
|
|
|
|
|
|
|
Conditions and Logical Expressions |
|
|
|
|
|
|
|
|
To ask a question in C++, we don't phrase it as a question; we state it as an assertion. If the assertion we make is true, the answer to the question is yes. If the statement is not true, the answer to the question is no. For example, if we want to ask, Are we having spinach for dinner tonight? we would say, We are having spinach for dinner tonight. If the assertion is true, the answer to the question is yes. If not, the answer is no. |
|
|
|
|
|
|
|
|
So, asking questions in C++ means making an assertion that is either true or false. The computer evaluates the assertion, checking it against some internal condition (the values stored in certain variables, for instance) to see whether it is true or false. |
|
|
|
|
|
|
|
|
In C++, assertions take the form of logical expressions (also called Boolean expressions*). Just as an arithmetic expression is made up of numeric values and operations, a logical expression is made up of logical values and operations. |
|
|
|
|
|
|
|
|
Here are some examples of logical expressions: |
|
|
|
|
|
|
|
|
A Boolean variable or constant |
|
|
|
|
|
|
|
|
An expression followed by a relational operator followed by an expression |
|
|
|
|
|
|
|
|
A logical expression followed by a logical operator followed by a logical expression |
|
|
|
|
|
|
|
|
Let's look at each of these in detail. |
|
|
|
|
|
|
|
|
Some programming languages include a data type named Boolean, which has only two literal constants: true and false. In these languages, if you declare a variable dataOK to be of type Boolean, you can store either the value true or the value false into the variable: |
|
|
|
 |
|
 |
|
|
*The word Boolean ('bool-e-un) is a tribute to George Boole, a nineteenth-century English mathematician who described a system of logic using variables with just two values, True and False. (See the May We Introduce box on page 198.) |
|
|
|
|
|