Introduction: Repetition

This chapter begins with its prerequisite, learning objectives, and overview.

We'll cover the following

Prerequisite

Decisions

Objectives

After completing this chapter, you should be able to:

  • Design a loop for a specific situation requiring repetition
  • Use a while statement to implement a given loop design
  • Identify logical errors in a given while loop
  • Control a while loop by using a Boolean variable
  • Write nested while loops

Overview

The applications we have seen so far read some data from a user, perform one or more computations, and display some results. To repeat the process for another set of data, we need to run the program again. Or do we? Java has the ability to repeat a group of statements. We can repeat them verbatim or with different values or data. We can repeat them a given number of times or until a certain condition is met. Perhaps the condition is the answer to a yes-or-no question asked of the user. The statements that provide for this repetition, like the decision statements discussed in previous chapters, allow deviation from the normal sequential execution of code.

Get hands-on with 1400+ tech skills courses.