What are loops in Java? And what are the types of loops?


To begin with let’s figure out what the loop is. The loop is any repetitive action.

For example, every morning you:

  1. get up
  2. go to the bathroom, brush the teeth, and then as luck would have it: someone shaves the legs, someone – the face
  3. open the fridge and eat everything that comes to hand
  4. get dressed
  5. go to the work or school

So at least 5 times a week (Monday-Friday) you do the same sequence of actions. And if the sequence of actions is the same, then we are talking about the loop. Well, now you know what the loop is.

In Java there are 4 loop types:

  1. while loop
  2. do…while loop
  3. for loop
  4. for each loop

At this stage it will be enough for you to know about first three loop types.

We will take a look at the for each loop a little bit later, in our other articles. Now we will only tell you, that the for each type is a variation of the for loop. For the first time the for each loop appeared in JDK5. In earlier versions of JDK this loop variation didn’t exist.

So it’s a high time to learn about the while loop.

While loop

This loop is sometimes called until” loop.

Let’s start with a fictional somewhat funny everyday example.

Let’s say you have a friend Charlie, and he has a girlfriend Katie. They are at the “rose and candy stage”, you know what it means. Charlie gives her flowers, invites to restaurants and buys ice-creams. In a word – he tries to cozy up to her.

But the budget of Charlie is limited. And he tells to himself: “I will invite Katie to restaurants and give her flowers until I have at least 10$ in my wallet. But as soon as I see that I have less than 10$ - it’s all over, we close the “rose and candy stage”, instead we will just sit on the couch at home, watch TV and so on”.

If you describe this life situation in the construction of the while loop (remember, sometimes the while loop is called “until” loop), this will look as follows:



In the example above:

  1. until” is while
  2. (in the wallet > = 10$) is the condition
  3. Invite my girlfriend to restaurants”, “Give her flowers” – these are the actions which will be executed while the condition is true.

So, the general form of the while loop in Java looks as follows:

Remember, the essence of the while statement is that it repeats the action(s) until the condition is true. That means, as long as Charlie has > = 10$, he will invite his girlfriend to restaurants and give her flowers. When the condition Charlie has > = 10$ becomes false, the repetition of actions “Invite to restaurants” and “Give her flowers” will stop and the loop will be completed.

It’s a high time to try to write a Java code using the while loop.

Task No.1

It’s necessary to display the numbers from 1 to 7, i.e. we should see the following at the screen: 1 2 3 4 5 6 7


Task No.2

And if we would write the code in the following way, it won’t be executed.

Why? Because the initial value of the int i =1, and we write while (i <o) in the condition. The i already equals 1, and therefore the i won’t be less than 0. Respectively, the code won’t be executed even once.

Task No.3

And if we would write the code in the following way, the numbers 1 2 3 4 5 6 7 would be displayed to infinity …

This is the example of so-called infinite loop, because the numbers will be displaying to infinity 1 2 3 4 5 6 7 8 9 10 11 12 13 … If you already tried to run this code on your computer, then you already noticed that the program was blocked by the infinite loop, because the code wouldn’t stop, instead it just continued to display the numbers at the screen. To get out of this situation you need to press the keyboard shortcut Alt + F4.

Why are the numbers displayed to infinity? Well, because we wrote while (true) in the code. And the condition will be true permanently. That’s why the numbers are displaying to infinity 1 2 3 4 5 6 7 8 9 10 11 12 13 …

Task No.4

It’s necessary to display the following number sequence at the screen:

100 90 80 70 60 50 40 30 20 10


Comments to the problem:

i-=10; is the shorthand for I = (i-10);

Here you will find more examples of tasks on the while loop.

Do…while loop

The do…while loop is one of the variations of the while loop. To understand the difference between do…while and while loops, let’s compare two examples of code.

Task No.5 – code with the while loop

Comment: this code won’t be executed. The reason for it is that in the first place the condition while ( i < 0 ) is checked, and the code executes only if the condition is true. I.e. the while loop kind of pre-checks the condition.

Task No.6 – code with the do…while loop

Comment: at first the while loop checks the condition and then executes the code (the loop body). Vice versa, the do…while loop first checks the loop body and only then the condition, that’s why the loop body executes at least for a one time. First the actions are specified and the condition while ( i < o) is carried out only after the execution of actions. That is, the number 2 will be displayed at the screen. And only after this while ( i < 0) will be checked, because the 2 > 0 loop will be completed. I.e. the do…while loop kind of pre-checks the condition.

General form of the do…while loop is as follows:

Remember: the do…while loop always executes at least for a one time. We have seen this clearly through the example above.

The do…while loop is a very good and convenient choice when you need to execute some action at least once, but under certain circumstances it needs to be repeated several times.

For loop

The for loop is also called the loop “with the counter”.

Let’s say we want to display the following message at the screen:





As you can see, the “awesome” word is used four times. It’s obvious that in this case you need to use the loop, because the same operation is repeated four times:

1. Firstly, we displayed the wordAwesome! at the screen

2. Secondly, we displayed the word Awesome! at the screen

3. Thirdly, we displayed the word Awesome! at the screen

4. Fourthly, we again displayed the wordAwesome! at the screen.

Remember: if you know in advance how many times the same action will be repeated – you need to use the for loop. For instance, in the example above we knew for sure that we needed to display the word “Awesome!” for four times. It means that you need to use the for loop.

Let’s analyze this code step by step:

The construction for (int i =1; i <=5; i++) in the Java language is like saying “repeat so many times”. In our case: “Repeat from 1 to 5, that is 5 times”.

int i = 1 is the first attribute, which is responsible for definition of the initial value. This attribute is called initialization, where i is the loop variable.

i <=5 is the second attribute, which is responsible for the condition under which the loop is executed. I.e. the loop will be executed as long as the condition (i.e. the variable i) doesn’t equal 5.

i++ is the third attribute, which is responsible for changing the variable i after each step of the loop. Our example states “i++”, it means that the variable will be increased by one in each step of the lop – from 1 to 2, from 2 to 3, from 3 to 4, from 4 to 5. This third attribute is called iteration.

General form of the for loop in Java is as follows:

We suggest you to solve one more problem.

Task No.7

It is necessary to display the numbers from 3 to -3 at the screen. I.e. the following numbers should be at the screen: 3 2 1 0 -1 -2 -3




Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--