Recommended Books

Java Loops



There are three types of Loops in Java, the while loops, do while and for loops. Click on the links to check individual articles on each loop, which deal with each loop in more detail.

We’ll discuss loop control statements and briefly go over each loop with an example.

StatementDescription
do whileExecutes a block of code once, and will keep repeating as long as a given given condition holds true.
whileRepeats a block a code while a given condition holds true.
for Repeats a block of code for a given number of times. Can also be used to iterate through collection types variables, like Arrays.

You can further increase the functionality of loops by creating “Nested loops”. Nested loop refers to a loop within a loop.

Java Loop control statements:

Loop control statement help us manipulate Java loops during their execution.

NameDescription
breakWhen a loop encounters this statement during its execution, it will immediately stop and “break”, moving to the next statement after the loop.
continueThe loop will stop it’s current iteration, and move on to the next iteration.

Java Loops Examples:

Some short examples to give you an idea of how each loop works. Follow the links for the detailed guide. Be vary of capitalization while writing loops, everything must be in lowercase.

While Loops:

While the condition x < 5 holds true, the loop will continue running. Once the condition x < 5 becomes false, the loop terminates.

int x = 0;

while (x < 5) {
    System.out.println(x);
    x = x + 1;
}
0
1
2
3
4

Do While Loops:

It is a variation of the While loop. Unlike the while loop, it will first execute the statements in the code block, and then proceed to check whether the given condition is true or false. If it was true, it will proceed to repeat itself.

int x = 0;
do {
    System.out.println(x);
    x = x + 1;
}
while (x < 5);

For Loops:

This for loops iterates over the list “shopping” outputting each element separately.

for (int i = 0; i < 5; i++) {									 
         System.out.println("Value of i is " + i);			
}
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4

Nested Loops:

A Nested loop is a loop within another loop. Using Nested loops in your code can prove very useful and help you iterate through sequences in new ways. The most common use of Nested loops comes in iterating through other Nested or multi-dimensional items like 2-D arrays.

What’s happening here is that there is a Array, which further contains two Arrays within it. This is what we call a two dimensional Array. If we iterate through it normally, we’ll just get 2 Arrays printed out, which you can even see in the example output. Iterating through the Array with two for loops gives us each element separately.

import java.util.Arrays;

public class example {
       public static void main(String[] args) {
	      String shopping[][] = {{"A", "B", "C"}, {"D","E","F"}};

	             for (int i = 0; i < 2; i++) {		             
                           System.out.println(Arrays.toString(shopping[i])); 
		                 for (int x = 0; x < 3; x++) {
		                        System.out.println(shopping[i][x]); 
		                 }
		     }                     		     		
	}
}

[A, B, C]
A
B
C
[D, E, F]
D
E
F

This marks the end of the Java Loops page. If you have any suggestions or contributions for Coders Legacy, please share them. Any questions can be asked in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *