Use a for Loop to Repeat Code

Computers are much better at repetitive tasks than humans are; you can tell the computer to do something ten times in a row and it will do exactly that, ten times, a hundred times, a million times, whatever you want. One way to do the same thing a bunch of times is to use a for loop. for is a Java keyword that lets you loop over a section of code a fixed number of times.

The for loop is a basic control structure in Java—a way to control the order of execution of your lines of code. If you need to make a bunch of blocks or spawn a lot of creepers in a Minecraft world, you’ll use a for loop. If you need to loop through all the players that are currently online, you’ll probably use a for loop (although there are ...

Get Learn to Program with Minecraft Plugins, 2nd 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.