قالب وردپرس درنا توس
Home / Tips and Tricks / How to use loops in Java

How to use loops in Java

Loops in Java

A loop is a structure in programming that allows you to run the same section of code over and over again. This can be used when you want to perform an iterative task (such as counting or sorting through a list) or to create an ongoing, cyclical experience for the user (as in a game loop). In this post, we will examine how to use loops in Java.

Also read: How to use loops in Python

For loops in Java

There are three types of loops in Java:

  • For loops
  • While loops
  • Make loops

Starting with loops, this is the best option for iterative tasks. The reason is that loops have a fixed number of iterations. We print this by first initializing a variable, then setting the condition and finally defining steps.

For example:

for (int n=1;n<=10;n++) {

Here we use the integer "n". The loop runs as long as n is less than or equal to 1. The value of n increases by one for each iteration.

Run this code and it simply counts from 1-10 on the screen.

If we said:

for (int n=1;n<=20;n+=2) {

We would see the numbers 1-19, count in two! And so on.

And we do not necessarily need to use integers:

   for (String hi = "hi";hi.length()<=10;hi = hi + "h") {

While looping in Java

While loops work just like for loops, except that you can determine all conditions using values ​​that you may have defined elsewhere in your code. To use while loops in Java, you simply need to add the condition in parentheses.

  int health=10;

  while(health > 0) {

Do while loops

A do while loop is very similar to a while loop, except that the structure is turned upside down so that the code block is always run at least once.

  } while(health > 0);

Here you see "health = 0" if the health is lower than one.

Which loop should you use when?

If all these different loops drive you with loops, it can be helpful to understand why we have three ways of doing basically the same thing.

  • For loops are useful when you have a fixed number of iterations. For example, this is a good way to run through a text document line by line. (First you need to get the number of rows.)
  • While loops are useful if you need something to run indefinitely until a certain condition is met. A good example can be your spell loop, which can run until the player hits the "exit" or runs out of health. Check out our post on how to write your first Android game in Java for a demonstration of how it can work.
  • Finally, while loops can be used when you want to run a code block at least once. In many user interface scenarios, for example, it can be useful to tell the user when there is nothing to do.

Of course, there are solvents to do any of these things with any of the different loops in Java! But choosing the right code for the job is the hallmark of efficient programming.

Java breaks and continues statements

Before you can claim to master loops in Java, you must deal with the statement "break". A pause message is your "get out clause" that effectively allows you to interrupt the loop at any time.

For example, you may want to run your game loop until the user dies, but also let them go out at any time. For this you can use the pause statement:

  int specialCondition = 3;
  int health=10;

  while(health > 0) {

    if (specialCondition == 3) {
      System.out.println("Special condition met!");


The expression "Continue" works in a similar way but allows you to return to the beginning of the loop and cut everything below. This can be useful to avoid complex, encapsulated statements within your loop.

There are other cool tricks we can pull off, such as nesting loops and the use of labels. These are topics we can come back to in the future. At the moment, you know all the important skills you need to start looping!

Also read: How to print a matrix in Java

Want more? Then check out our free, comprehensive Java beginner course to get a great idea of ​​the language. Alternatively, you can find lots of full courses by checking out our list of the best resources for learning Java.

Source link