For loops in Java are just one type of loop that can be used to repeat a block of code for iterative operations. For example, if you need to open a text file, you can use a loop to iterate through each line of the document and print it on the screen.
Likewise, games often use a “game loop” that repeats itself each time the frame is updated to look for input and update the locations of enemies, physical objects, and so on.
See also: Using loops in Java
For loops in Java are extremely powerful and useful for many different coding applications. In this post, we’ll explain how to use them and consider more advanced concepts like labeling.
Use for loops in Java
What distinguishes a for loop from other types of loops in Java is the fixed number of iterations. The syntax for a for loop in Java is:
for (declare variable; condition; increment) {
Everything in the curly braces (code block) is then executed repeatedly until the condition is met. For example:
for (int n=1; n<=100; n++) { System.out.println(n); }
This counts to 100 and prints the numbers on the screen.
Pause and on
Now you understand the basics of for loops in Java. However, there are a number of advanced concepts that you can use to write smarter code and better loops.
For example: break and continue.
Break is a keyword that ends the loop at any point. This can be useful if, for example, you want to have the user press esc to stop the game.
for (int n=1; n<=100; n++) { System.out.println(n); if (n == 30) { break; } }
In the meantime the loop will restart at the beginning. This means that you can skip part of the code block for a given iteration.
See also: How to write your first Android game in Java
In a gaming scenario, this can be useful when the player presses “pause”.
Nested for loops
Nothing prevents you from playing a for loop inside a for loop. This is called a “nested loop,” and you can repeat this process as many times as you think necessary.
for (int n=1; n<=10; n+=10) { System.out.println(n); for (int i=1; i<=100; i++) { System.out.println(i * n); } }
The following counts to 100, but shows the number “1” twice.
The only problem with this is that if you use break or continue at any point, you’ll break out every level.
Labels are a useful tool that can be used for for loops in Java. Labels let you choose exactly which loop you want to break and where you want to go in your code. You use them simply by choosing a name for your loop and then adding it with a colon just before your loop code.
public static void main(String []args){ outerloop: for (int n=1; n<=10; n+=10) { System.out.println(n); innerloop: for (int i=1; i<=100; i++) { System.out.println(i * n); if (i == 50) { break innerloop; } } } }
Now you are up to date!
By now you should know how to use for loops in Java.
If you really want to develop your coding skills, don’t forget to check out our list of the best resources for learning Java. There you can find courses like the Complete Java Bundle, the Android Authority Readers can sign up for just $ 39. That’s a huge 96% discount on the US dollar