Chapter 5

Going Around in Circles (Or, Using Loops)

IN THIS CHAPTER

check The thrill of while loops

check The rapture of infinite loops

check The splendor of do loops

check The joy of validating input

check The wonder of for loops

check The ecstasy of nested loops

So far, all the programs in this book have started, run quickly through their main method, and then ended. If Dorothy from The Wizard of Oz were using these programs, she’d probably say, “My, programs come and go quickly around here!”

In this chapter, you find out how to write programs that don’t come and go so quickly. They hang around by using loops, which let them execute the same statements more than once.

Loops are the key to writing one of the most common types of programs: programs that get input from the user, do something with it, get more input from the user and do something with that, and keep going this way until the user has had enough.

Put another ...

Get Java All-in-One For Dummies, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.