Loops in Java

Every programming language has some loop. A programming language is incomplete without loop and it is most common and widely used in code. The basic concept of language starts from the loop. So in this article we will see loops in java.

Loops in java are used to execute the block of statement repeatedly until the condition is true. By using the loop, we can execute the code several times or until it is satisfying the condition.
Loops are very useful when a programmer wants to execute a statement or block of statement multiple times. In this article, we will discuss all the Loops in Java. Basically loops are part of the Control structure of Java. By the use of loops in java we can control the execution of code and decide how many times code should be executed.

Here is the table content of the article will we will cover this topic.
1. for loop
2. while loop
3. do-while loop

Loops in Java

Three types of Loops in Java

for loop

If a user wants to execute the block of code several times. It means the number of iterations is fixed. JAVA provides a concise way of writing the loop structure. For loop consists of 4 parts i.e. Initialization, condition, Body of for loop, and Increment/decrement. We will discuss it in detail.

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Declaring a int Array with with values
		int[] number = new int[5];
		
		// Let's Initialize the values of Array by for loop
		for(int i = 0; i < number.length; i++)
		{
			number[i] = 10 + i;
		}
		
		System.out.println("Print int Array:");
		for(int i = 0; i < number.length; i++)
		{
			System.out.println(number[i]);
		}
	}
}

Output: Print int Array:
10
11
12
13
14

while loop

while loop allows code to be executed repeatedly until a condition is satisfied. If the number of iterations is not fixed, it is recommended to use a while loop. It is also known as an entry control loop. We will discuss it in detail.

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Declaring a int Array with with values
		int[] number = new int[5];
		
		// Let's Initialize the values of Array by while loop
		int i = 0; 
		while(i < number.length)
		{
			number[i] = 10 + i;
			i++;
		}
		
		System.out.println("Print int Array:");
		i = 0;
		while(i < number.length)
		{
			System.out.println(number[i]);
			i++;
		}
	}
}

Output: Print int Array:
10
11
12
13
14

do-while loop

It is like while loop with the only difference that it checks for the condition after the execution of the body of the loop. It is also known as Exit Control Loop. We will discuss it in detail.

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Declaring a int Array with with values
		int[] number = new int[5];
		
		// Let's Initialize the values of Array by do while loop
		int i = 0; 
		do 
		{
			number[i] = 10 + i;
			i++;
		}
		while(i < number.length);
		
		System.out.println("Print int Array:");
		i = 0;
		do 
		{
			System.out.println(number[i]);
			i++;
		}
		while(i < number.length);
	}
}

Output: Print int Array:
10
11
12
13
14

Leave a Comment