All about Java loops
This post will shortly introduce to you all known Java loops: for, while, do while, for each. Yes-yes, Java as many other programming languages has its own loop constructions. So the time has come to find out or just remind you how can every above mentioned loop be used.
But firstly I would like to explain where loops are applied. Programs often contain logic which requires numerous repeats of similar operations (e.g. go through a set of values). To avoid duplication of identical code you’d better use loops. In this way your code becomes more flexible, maintainable and readable.
Let’s look over every loop construction individually.
Loop “for”
The following syntax has this kind of loop:
for (start condition; boolean expression; rule for changing of the start condition) { ... }
To be more concrete let’s proceed to the example:
for(int i = 0; i < 10; i++) { System.out.print(i+" "); }
The program output is: 0 1 2 3 4 5 6 7 8 9
We can use “for” to go through an array:
int[] array = {0, 1, 2, 3, 4, 5}; int length = array.length; for(int i = 0; i < length; i++) { System.out.print(array[i]+" "); }
The program output is: 0 1 2 3 4 5
Loop “while”
This kind of loop works a little bit differently than the loop “for”. To show the main distinction I prepared an example:
int i = 0; while (i < 10) { System.out.print(i+" "); i++; }
The program output is: 0 1 2 3 4 5 6 7 8 9
The “while” loop doesn’t specify precisely the number of iterations unlike the “for” loop. Instead of that it checks a boolean expression before every step. To impact on boolean expression you need to put additional logic in the loop body (in our example it was i++).
int[] array = {0, 1, 2, 3, 4, 5}; int length = array.length; int i = 0; while (i < length) { System.out.print(array[i]+" "); i++; }
The program output is: 0 1 2 3 4 5
Loop “do while”
This kind of loop works as the previous one, just with one difference – it checks boolean a expression after each step. That means that the “do while” loop always performs at least one step.
Classic example of usage:
int i = 0; do { System.out.print(i+" "); i++; } while (i < 10);
The program output is: 0 1 2 3 4 5 6 7 8 9
One more example of usage with arrays:
int[] array = {0, 1, 2, 3, 4, 5}; int length = array.length; int i = 0; do { System.out.print(array[i]+" "); i++; } while (i < length);
The program output is: 0 1 2 3 4 5
Loop “for each”
This type of loop is applicable just for iterating through arrays or lists (or other data structures which implements interface Iterable).
int[] array = {0, 1, 2, 3, 4, 5}; for (int element : array) { System.out.print(element+" "); }
The program output is: 0 1 2 3 4 5
For more in-depth understanding of this example, read the code as follows: for the each element from the array…
Summary
Loops in Java are very powerful structures that give you the possibility to implement everything you want related to the iteration process. Different types of loops allow you to write code in the most convenient way depending on situation. Don’t forget to specify correct operating conditions for loops to avoid the infinite loop.