6.1: Introduction

CS 113 Programming in Java · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under not declared, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    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 and if/else statements 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 switch statement, 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 for statement.

    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 while and do-while statements. 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.


    6.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.