Hour 8. Repeating an Action with Loops

One of the more annoying punishments for schoolchildren is to make them write something over and over again on paper or a chalkboard.

On The Simpsons, in one of his frequent trips to the board, Bart Simpson had to write, “I will not obey the voices in my head” dozens of times. This kind of punishment might work on children, but it definitely would fail to be punitive to a computer, which can repeat a task with ease.

Computer programs are ideally suited to do the same thing over and over again because of loops. A loop is a statement or set of statements that will be repeated in a program. Some loops take place a fixed number of times. Others take place indefinitely.

There are three loop statements in Java: ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION 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.