Chapter 5

Using Loops in Java Code

In Chapter 4, “Using Java Arrays,” we used the while construct to populate an array with a section of repeating code. Without the means to loop through code the way we did, a task like populating an array is still possible, just far more cumbersome to implement. Loops can make any tedious, repetitive task easier to express in code. In this chapter, we’ll look at ways to use loops to make such tasks simpler and more economical.

Java supports four loop constructs. We’ll look at each one, compare them, and consider how best to apply each one. We’ll also look at how to break a loop when we arrive at odd conditions we can’t handle nicely in repeating code.

In this chapter, we’ll cover the following topics:

  • Applying ...

Get Java SE 7 Programming Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.