9. Repeat an Action with Loops

In This Chapter

Use the for loop

Use the while loop

Use the do-while loop

Exit a loop prematurely

Name a loop

A computer program can repeat a task with ease. Programs are ideally suited to do the same thing over and over because of loops. A loop is a statement or block that is repeated in a program. Some loops run a fixed number of times. Others run indefinitely.

There are three loop statements in Java: for, do, and while. Each can work like the others, but it’s beneficial to learn how all three operate. You often can simplify a loop section of a program by choosing the right statement.

for Loops

In your programming, you find many circumstances in which a loop is useful. You can use them to keep doing ...

Get Absolute Beginner’s Guide to Minecraft® Mods Programming, Second Edition now with O’Reilly online learning.

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