5.1. Introduction
In this chapter, we introduce C++'s
remaining control statements. The control statements we study here and in Chapter
4 will help us build and manipulate objects. We
continue our early emphasis on object-oriented programming that began with a
discussion of basic concepts in Chapter
1 and the extensive object-oriented code examples in Chapters
3–4.
In this chapter, we demonstrate the for,
do...while and switch statements. Through a series of short
examples using while and for, we explore the essentials of
counter-controlled repetition. We expand the GradeBook class presented
in Chapters
3–4.
In particular, we create a version of class GradeBook that uses a
switch statement to count the number of A, B, C, D
and F grades in a set of letter grades entered by the user. We introduce the
break and continue program control
statements. We discuss the logical operators, which enable you to use more
powerful conditional expressions in control statements. We also examine the
common error of confusing the equality (==) and assignment (=)
operators, and how to avoid it.