Section 6.1 Introduction
As we learned in Chapter 3, a control structure is a language element that changes the flow of control of a program. Thus far, we have used the
if/elsestatements to select between two or more alternate paths in a program. We have used the
while-loop structure to repeat statements. And we have used method-call-and-return to invoke methods that carry out certain well-defined tasks in a program.
In this chapter we will extend our repertoire of control structures. We will introduce the for and do-while statements, both of which are used in programs that require calculations to be repeated. We will also introduce the
switchstatement, which will give us another way, in addition to
if/else, to select from among several alternate paths in a program.
We begin by introducing the idea of a counting loop, which is used for repetitive tasks when you know beforehand exactly how many repetitions are necessary. This type of loop is most often implemented using a
We then distinguish two kinds of conditional loops, which are used for performing repetitive tasks where the number of repetitions depends on some kind of non-counting condition. These kinds of loops are usually implemented using Java’s
do-whilestatements. We give examples of several kinds of loop bounds and use them to identify several useful principles of loop design. Finally, we introduce some of the key principles of the structured programming approach, a disciplined design approach that preceded the object-oriented approach.
You have attempted of activities on this page.