Loops in Java [ For, While & Do While ] with Examples

Loops are an important tool used in programming languages in order to execute a set of functions again and again after some expressions become true. The main types of loops [ while & do while ] used in Java.

Loops in Java

A for loop is used to provide a short and easy way to debug the stricter of the loop in Java. It writes the structure of the loop in a concise was and checks the condition in one line. Java for loop is further divided into three types. They are:

Simple for loop in java

Simple for loop is used to initiate a variable and check condition. It is similar to a for loop in other programming languages and checks the increment or decrement value in the same way.

syntax:

for(<initialization>;<loop condition>;<increment/decrement expression>){
//executable statements }

For loop is a looping construct that executes a set of instructions specified number of times.

Example:

public class ForLoopDemo {

	public static void main(String[] args) {

		for (int i = 0; i < 10; i++) {
			System.out.println("Value of i is : " + i);
		}
	}
}

 for-each loop

Java for-each loop doesn’t need the increment value so it is easier to use than simple for loop. It uses subscript notation and works on elements basis. It does not work on an index and is used to return the element one by one in a defined variable and traverses an array or a collection.

Syntax:

for (data type : array) {
// statement }

Example:

package com.javafolder.loops.example;

import java.util.*;

public class ForEachDemo {

	public static void main(String args[]) {

		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(10);
		list.add(20);
		list.add(30);
		list.add(40);
		list.add(50);
		list.add(60);
		list.add(70);
		list.add(80);
		list.add(90);
		list.add(100);

		for (Integer s : list) {
			System.out.println("Value of s : "+ s);
		}
	}
}

 Labeled For Loop

This loop is a type of for loop that is labeled and is used for a specific purpose. It is used to break or continue the loop in a specific condition.

Example:

public class LabeledForLoop {

	public static void main(String[] args) {

		int number = 0;

		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 5; j++) {
				if (i == 5)
					break;
			}
			number++;
		}

		System.out.println(number);
	}
}

While Loop

While Loop is used when the number of iteration is not fixed. It performs a part of the program many times.

Syntax:

while<loop condition>
//statements

public class WhileLoopDemo {

	public static void main(String[] args) {

		int number = 0;

		while (number < 10) {
			number++;

			System.out.println(number);

		}

	}
}

 Do While Loop

Like the while loop, do while loop is also used to perform a part of the program several times. The do while loop is used when the loop must be executed at least a single time and the number of iteration is not fixed.

public class DoWhileDemo {

	public static void main(String[] args) {

		int a = 1;
		int b = 2;
		int x = a + b;
		do {
			System.out.println(x);
			x++;
		} while
		(x <= 10);
	}
}

Java Infinite do-while Loop

If a statement is passed as true in a do-while loop, the loop will become an infinite do-while loop.

Example:

package Collection;

public class InfiniteDoWhileLoop {

	public static void main(String[] args) {
		do {
			System.out.println("I am always looping...");

		} while (true);
	}
}

Leave a Reply

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