Java — Loops
Loops, in a programming language, is a feature used to execute a particular part of the program repeatedly if a given condition evaluates to be true.
Loops are handy because they save time, reduce errors, and they make code more readable.
There are 2 main types of loops:
- while loops(with do/while loops)
- for loops(with nested loops and for-each loops)
While Loops
The while loop loops through a block of code as long as a specified condition is true:
while (condition) {
// code block to be executed
}In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 10:
int i = 1;
while (i < 5) {
System.out.println(i);
i++;
}Output:
0
1
2
3
4Do/While Loops
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
do {
// code block to be executed
}
while (condition);The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);Output:
0
1
2
3
4Note: For the
whileanddo/whileloops, do not forget to increase the variable used in the condition, otherwise the loops will never end!
For Loops
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}Statement 1 is executed (one time) before the execution of the code block.
Get Sithila Sihan Somaratne’s stories in your inbox
Join Medium for free to get updates from this writer.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Output:
0
1
2
3
4Example explained
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has been executed.
Another is example is this one below:
for (int i = 0; i <= 10; i = i + 2) {
System.out.println(i);
}The above code will only print even values between 0 and 10 as shown below:
0
2
4
6
8
10Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
The “inner loop” will be executed one time for each iteration of the “outer loop”:
// Outer loop
for (int i = 1; i <= 2; i++) {
System.out.println("Outer: " + i); // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; j++) {
System.out.println(" Inner: " + j); // Executes 6 times (2 * 3)
}
}Output:
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3For-Each loops
There is also a “for-each” loop, which is used exclusively to loop through elements in an array (or other data sets):
for (type variableName : arrayName) {
// code block to be executed
}The following example outputs all elements in the cars array, using a “for-each” loop:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String car: cars) {
System.out.println(car);
}Note: Don’t worry if you don’t understand the example above. You will learn more about Arrays in a later blog.






