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 Minecraft Mods Programming Absolute Beginner’s Guide 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.