Introduction: Repetition Continued

We begin with this chapter's prerequisite, learning objectives, and overview

We'll cover the following



After completing this chapter, you should be able to:

  • Write a for loop or a do loop for a specific situation
  • Transform a for loop into a while loop
  • Identify logical errors in a given fof loop or do loop
  • Write nested for loops or do loops


The while statement that we introduced in the previous chapter controls the repetition of any group of Java statements. Although we do not need any other way to control a loop, Java provides two other statements for this purpose. When a loop is controlled by counting, the for statement provides a convenient alternative to the while statement. And if we know that the body of a loop will execute at least once, we can use a do statement instead of a while statement. This chapter discusses both of these alternative loop-control statements.

Get hands-on with 1200+ tech skills courses.