Java for students logo
Explore the English language on a new scale using AI-powered English language navigator.

Loop statements in Java

In previous articles you learned about variables and types and now it's time to speak about control statements in Java. There are several types of statements in Java, which are useful for repeating things. Before we are going any further let's check simple example printing "Hello!" thing to the console ten times:

public class Loops {

      public static void main(String[] args) {

            System.out.println("I am about to print \"Hello!\" ten times.");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Hello!");

            System.out.println("Uff... It was long. Is there a way to compact repeating things?");

      }

}

I am about to print "Hello!" ten times.
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Uff... It was long. Is there a way to compact repeating things?

It was long, wasn't it? In a big program one may need to repeat things hundreds of times or, most often get known how many times things are to be repeated only during the program run. Let's compact the code using for loop.

public class Loops {

      public static void main(String[] args) {

            System.out.println("Let's compact the code using for loop.");

            int i;

            for (i = 0; i < 10; i++)

                  System.out.println("Hello!");

            System.out.println("Now the code looks much better.");

      }

}

Let's compact the code using for loop.
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Now the code looks much better.

Learning the magic of the for loop

The for statement consists of three elements:

  1. Initialization (i = 0): initializes counter, executed once before the loop begins.
  2. Termination (i < 10): loop repeats, while this expression is correct. It stops, when it becomes false.
  3. Increment (i++): increments counter.

In the example above, counter i takes on the following values:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Hence, the loop's body is repeated ten times. Using counter you may track the current step:

public class ForLoop {

      public static void main(String[] args) {

            System.out.println("Let's count to 10:");           

            for (int i = 1; i <= 10; i++)

                  System.out.println(i);      

      }

}

Let's count to 10:
1
2
3
4
5
6
7
8
9
10

There are slight changes in the code we would like to comment.

  1. One may declare counter inside the initialization statement (int i = 1). If you do so, counter variable will be visible inside a for statement only.
  2. Notice that in the example above i starts with 1 and loops, while i <= 10. 10 is included now, in contrast to the previous example.

Note. One is allowed to change counter inside the for loop, but it is considered as a bad programming style.

public class ForLoop {

      public static void main(String[] args) {

            System.out.println("Let's count to 10:");

            for (int i = 1; i <= 10; i += 2) {

                  System.out.println(i);

                  // bad programming style

                  i--;

            }

      }

}

The while and do-while statements

The while statement executes its body, while specified condition is true. To be more illustrative, let's rewrite the counting example:

public class WhileLoop {

      public static void main(String[] args) {

            System.out.println("Let's count to 10 using while:");

            int i = 1;

            while (i <= 10) {

                  System.out.println(i);

                  i++;

            }

      }

}

Let's count to 10 using while:
1
2
3
4
5
6
7
8
9
10

Notice that while statement checking the condition before it executes body for the first time. So, it's possible, that it wouldn't execute at all:

public class WhileLoop {

      public static void main(String[] args) {

            System.out.println("Start counting...");

            int i = 11;

            while (i <= 10) {

                  System.out.println(i);

                  i++;

            }

            System.out.println("It seems that no lines have been printed.");

      }

}

Start counting...
It seems that no lines have been printed.

The do-while statement checks the condition after it executes body for the first time. Hence, it always executed at least one time, even if the condition is always false:

public class DoWhileLoop {

      public static void main(String[] args) {

            System.out.println("Start counting...");

            int i = 1;

            do {

                  System.out.println(i);

                  i++;

            } while (false);       

      }

}

Start counting...
1

Note. The for loop checks it's condition before:

public class ForLoop {

      public static void main(String[] args) {

            System.out

                        .println("Will the following for loop repeat at least once?");

            for (int i = 1; i < 1; i++) {

                  System.out.println("Repeated!");

            }

            System.out.println("No, it doesn't.");

      }

}

Will the following for loop repeat at least once?
No, it doesn't.

Congratulations! Now, you have one more tool in your programming inventory.