C - Programming - C - Program - Chapter 04.pdf

(815 KB) Pobierz
CHAPTER
4
C ONTROL S TRUCTURES I
(S ELECTION )
IN THIS CHAPTER, YOU WILL:
n Learn about control structures
n Examine relational and logical operators
n Explore how to form and evaluate logical (Boolean) expressions
n Discover how to use the selection control structures if ,
if ... else , and switch in a program
n Learn to use the assert function to terminate a program
1247350645.051.png 1247350645.061.png 1247350645.069.png 1247350645.070.png 1247350645.001.png 1247350645.002.png 1247350645.003.png 1247350645.004.png 1247350645.005.png 1247350645.006.png 1247350645.007.png 1247350645.008.png 1247350645.009.png 1247350645.010.png 1247350645.011.png 1247350645.012.png 1247350645.013.png 1247350645.014.png 1247350645.015.png 1247350645.016.png 1247350645.017.png 1247350645.018.png 1247350645.019.png 1247350645.020.png 1247350645.021.png 1247350645.022.png 1247350645.023.png 1247350645.024.png 1247350645.025.png 1247350645.026.png 1247350645.027.png 1247350645.028.png 1247350645.029.png
168
|
Chapter 4: Control Structures I (Selection)
Chapter 2 defined a program as a sequence of statements whose objective is to
accomplish some task. The programs youhaveexaminedsofarweresimple
and straightforward. To process a program, the computer begins at the first exe-
cutable statement and executes the statements in order until it comes to the end.
In this chapter and Chapter 5, you will learn how to tell a computer that it does
not have to follow a simple sequential order of statements; it can also make
decisions and repeat certain statements over and over until certain conditions
are met.
Control Structures
A computer can process a program in one of the following ways: in sequence; selectively,
by making a choice, which is also called a branch; repetitively, by executing a statement
over and over, using a structure called a loop; or by calling a function. Figure 4-1
illustrates the first three types of program flow. (In Chapter 7, we will show how function
calls work.) The programming examples in Chapters 2 and 3 included simple sequential
programs. With such a program, the computer starts at the beginning and follows the
statements in order. No choices are made; there is no repetition. Control structures
provide alternatives to sequential program execution and are used to alter the sequential
flow of execution. The two most common control structures are selection and repetition.
In selection, the program executes particular statements depending on some condition(s).
In repetition, the program repeats particular statements a certain number of times based on
some condition(s).
statement1
expression
false
true
statement
expression
true
statement2
statement2
statement1
false
statementN
a. Sequence
b. Selection
c. Repetition
Flow of execution
FIGURE 4-1
1247350645.030.png 1247350645.031.png 1247350645.032.png 1247350645.033.png 1247350645.034.png 1247350645.035.png 1247350645.036.png 1247350645.037.png 1247350645.038.png 1247350645.039.png 1247350645.040.png 1247350645.041.png 1247350645.042.png 1247350645.043.png 1247350645.044.png 1247350645.045.png 1247350645.046.png 1247350645.047.png 1247350645.048.png 1247350645.049.png 1247350645.050.png 1247350645.052.png
 
Relational Operators
|
169
Before you can learn about selection and repetition, you must understand the nature
of conditional statements and how to use them. Consider the following three
statements:
1. if (score is greater than or equal to 90)
grade is A
2. if (hours worked are less than or equal to 40)
wages = rate * hours
otherwise
wages = (rate * 40) + 1.5 *(rate *(hours – 40))
3. if (temperature is greater than 70 degrees and it is not
raining)
Go golfing!
These statements are examples of conditional statements. You can see that certain
statements are to be executed only if certain conditions are met. A condition is met if
it evaluates to true . For example, in statement 1:
4
score is greater than or equal to 90
is true if the value of score is greater than or equal to 90 ;itis false otherwise. For
example, if the value of score is 95 , the statement evaluates to true . Similarly, if the
value of score is 86 , the statement evaluates to false .
It would be useful if the computer could recognize these types of statements to be true
for appropriate values. Furthermore, in certain situations, the truth or falsity of a
statement could depend on more than one condition. For example, in statement 3, both
temperature is greater than 70 degrees and it is not raining must be true
to recommend golfing.
As you can see, for the computer to make decisions and repeat statements, it must be able
to react to conditions that exist when the program executes. The next few sections discuss
how to represent and evaluate conditional statements in C++.
Relational Operators
To make decisions, you must be able to express conditions and make comparisons. For
example, the interest rate and service charges on a checking account might depend on the
balance at the end of the month. If the balance is less than some minimum balance, not
only is the interest rate lower, but there is also usually a service charge. Therefore, to
determine the interest rate, you must be able to state the minimum balance (a condition)
and compare the account balance with the minimum balance. The premium on an
insurance policy is also determined by stating conditions and making comparisons. For
example, to determine an insurance premium, you must be able to check the smoking
status of the policyholder. Nonsmokers (the condition) receive lower premiums than
1247350645.053.png
170
|
Chapter 4: Control Structures I (Selection)
smokers. Both of these examples involve comparing items. Certain items are compared
for equality against a particular condition; others are compared for inequality (greater than
or less than) against a particular condition.
In C++, a condition is represented by a logical (Boolean) expression. An expression that
has a value of either true or false is called a logical (Boolean) expression. More-
over, true and false are logical (Boolean) values. Suppose i and j are integers.
Consider the expression:
i > j
If this expression is a logical expression, it will have the value true if the value of
i is greater than the value of j ;otherwise,itwillhavethevalue false .The
symbol > is called a relational operator. A relational operator allowsyoutomake
comparisons in a program.
C++ includes six relational operators that allow you to state conditions and make
comparisons. Table 4-1 lists the relational operators.
TABLE 4-1
Relational Operators in C++
Operator
Description
==
equal to
!=
not equal to
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
In C++, the symbol == , which consists of two equal signs, is called the equality operator.
Recall that the symbol = is called the assignment operator. Remember that the equality
operator, == , determines whether two expressions are equal, whereas the assignment
operator, = , assigns the value of an expression to a variable.
Each of the relational operators is a binary operator; that is, it requires two operands.
Because the result of a comparison is true or false , expressions using these operators
evaluate to true or false .
1247350645.054.png 1247350645.055.png 1247350645.056.png 1247350645.057.png 1247350645.058.png 1247350645.059.png 1247350645.060.png 1247350645.062.png 1247350645.063.png 1247350645.064.png 1247350645.065.png
 
Relational Operators
|
171
Relational Operators and Simple Data Types
You can use the relational operators with all three simple data types. For example, the
following expressions use both integers and real numbers:
Expression
Meaning
Value
8 < 15
8 is less than 15
true
6 != 6
6 is not equal to 6
false
2.5 > 5.8
2.5 is greater than 5.8
false
4
5.9 <= 7.5
5.9 is less than or equal to 7.5
true
Comparing Floating-point Numbers for Equality
Comparison of floating-point numbers for equality may not behave as you would expect;
see Example 4-1.
EXAMPLE 4-1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << showpoint << setprecision(17);
cout << "3.0 / 7.0 = " << (3.0 / 7.0) << endl;
cout << "2.0 / 7.0 = " << (2.0 / 7.0) << endl;
cout << "3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 = "
<< (3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0) << endl;
return 0;
}
Sample Run:
3.0 / 7.0 = 0.42857142857142855
2.0 / 7.0 = 0.28571428571428570
3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 = 0.99999999999999989
From the output, it follows that the following equality would evaluate to false .
1.0 == 3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0
The preceding program and its output show that you should be careful when comparing
floating-point numbers for equality. One way to check whether two floating-point numbers
are equal is to check whether the absolute value of their difference is less than a certain
1247350645.066.png 1247350645.067.png 1247350645.068.png
Zgłoś jeśli naruszono regulamin