Chapter 7 review. Are the following statements true or false? Explain why or why not.
a) An empty statement
is a statement without any meaning.
False. An empty statement
does mean something, i.e., do nothing, then go on to the next statement;
this may be useful in correctly structuring nested structures. (See page
185.)
b) In a nested if-else,
the indentation shows which if clause each else clause goes
with.
False. The computer does
not notice the indentation, but the reader does, and may be misled. (See
page 184.)
c) The C++, the truth values
T and F are always the integers 1 and 0, respectively.
False. The value for F
is indeed 0, but the value for T is any non-zero number. (See page 186.)
d) An inclusive disjunction
is true when exactly one of its disjuncts is true.
False. An inclusive disjunction
is true when at least one of its disjuncts is true. Put another
way, an inclusive disjunction is false when both its disjuncts are false.
(See page 189.)
e) The negation of a conjunction
is equivalent to the conjunction of the negations of the conjuncts.
False. The negation of
a conjunction is equivalent to the disjunction of the negations
of the conjuncts. (See page 190.)
f) In the C++ expression
0 < x && x < 100,
the conjunction is evaluated first.
False. C++ gives relational
operators (such as ‘<’) a higher priority than logical operators (See
page 191.)
g) A C++ compiler would
reject the expression 0 < x < 100.
False. The leftmost inequality
would be evaluated, and the resulting (numerical!) value would be compared
with 100.
The result would almost certainly be an error. (See pages 191-192.)
h) In C++, unary operators
are evaluated before binary operators.
True. Although there are
certain special binary operators (to be discussed later) that do precede
most of the unary operators. (See pages 192, 549-550.)
i. A menu-driven program
is one that deals with food choices.
False. It's one that presents
users with a list (or menu) of options and asks the user to choose from
among them, until the user makes a valid choice. (See pages 212-214.)
j. In a while
loop, the loop body always executes at least once.
False. If the conditional-expression
happens to evaluate to false (0) the first time, the loop body will never
be executed. If you want a loop that executes at least once, use
a do..while,
described in the Beyond C++-- section at the end of this chapter (See pages
202-204 and 232-233.)
k. In a while
loop expressing a definite repetition, the use of a control variable is
essential.
True. A definite
loop increments and tests the loop control variable (or counter).
A typical test for a definite loop is counter
> final_value, where
counter
is a loop control variable. (See pages 205-206.)
l. In a nested loop, the
outer loop is performed before the outer loop.
True. Control enters
the outer loop first, then the inner loop nested within the outer loop.
Once the inner loop completes, there may be more statements in the outer
loop to execute, then control may continue again at the top of the outer
loop. (See pages 210-211.)
m. If both the inner and
outer loops are governed by counters, the counters should be different.
True. Resetting a
counter variable in more than one place is usually a logic error.
(See page 238, common bug #16.)
n. Menu-driven programs
shuold never contain loops.
False. It's good
user interface design to keep giving the user another chance to enter valid
input, rather than give up. (See pages 212-214.)
o. In a sentinel-controlled
loop, there should be an initial Read before the loop executes.
True. The first Read sets
up the entry condition, just in case the sentinel is the first item read.
(See pages 214-216.)
p. Loops are of great help
in error trapping.
True. Error trapping
ensures that only valid data are entered. A loop can check for invalid
data, ignore it, and continue to look for more data. (See page 216.)
q. File-processing programs
may involve both reading from a file and writing to a file.
True. However, the
examples discussed in the book deal exclusively with reading from a file.
It's often useful to read from an external file and write output to standard
output (usually the screen). (See pages 216-219.)
r. The function eof()
ends a program if it encounters an end-of-file character.
False. The function
eof()
returns a bool
value of true
if it encounters an end-of-file character. Typically !eof()is
the conditional-expression of a while
loop. The loop exits when eof()
returns true;
further processing may follow, such as closing an external file.
(See page 218.)
s. Every loop contains at
least one bug.
False. We hope!
;-) Loops are, however, a common cause for bugs. Study the
Common Bugs section of this chapter as well as the multimedia coverage
of bugs. (See pages 237-239.)
t. Putting output statements
into loops is a good way to squash bugs.
True. One way to
debug a loop is to insert output statements that show the value of variables
that change in the loop, so you can observe the behavior of the loop.
(See page 221.)
u. Writing and confirming
loop invariants is a good way to keep bugs out.
True. Loop invariants
provide an independent notation describing what a loop is intended to do.
Making sure the loop actually does what it is supposed to do can provide
headaches from logic errors later. (See pages 207-209 and 220-221.)