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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access