3 types of for loop in Java

In this article, we will discuss different types of for-loop evolved over years in various Java versions

Starting Java 1.1 version, for-loop was always there to iterate over list of items or collection of items. This old for-loop is referred as traditional or standard for-loop by Java community

Over the years, there are different versions of for-loop has been introduced by SUN/Oracle team. Till Java 1.4 version, everyone has to iterate over collection of items using standard for-loop only

But in later versions, improved for-loop has been added to JDK. We will list them below and also discuss with an example

 

3 types of for-loop in Java:

  1. Standard or traditional for-loop from very 1st version
  2. Enhanced forEach introduced in Java 1.5 version
  3. Iterable’s forEach loop introduced in Java 1.8 version

 

Let us move forward to discuss all 3 types with an example

Java 1.1: Standard or Traditional for-loop

Syntax:

for (initialization; termination; increment) {
      statement(s);
}

IteratingUsingStandardForLoop.java

package in.bench.resources.various.forloop.types;

import java.util.ArrayList;
import java.util.List;

public class IteratingUsingStandardForLoop {

	public static void main(String[] args) {

		// invoke methods
		iteratingCollection();
		iteratingArrays();
	}

	public static void iteratingCollection() {

		// create collection to store player names
		List<String> players = new ArrayList<String>();

		// add player names to list
		players.add("Mithali Raj");
		players.add("Harmanpreet Kaur");
		players.add("Veda Krishnamurthy");
		players.add("Smriti Mandhana");
		players.add("Poonam Raut");
		players.add("Jhulan Goswami");
		players.add("Deepti Sharma");

		// iterate using standard for-loop
		System.out.println("Iterating Collection using standard for-loop: \n");
		for(int index=0; index<players.size(); index++) {
			System.out.println(players.get(index));
		}
	}

	public static void iteratingArrays() {

		// create Integer Arrays
		Integer[] ramSize = new Integer[]{32, 64, 128, 256, 512};

		// iterate using standard for-loop
		System.out.println("\n\nIterating Arrays using standard for-loop: \n");
		for(int index=0; index<ramSize.length; index++) {
			System.out.println(ramSize[index]);
		}
	}
}

Output:

Iterating Collection using standard for-loop: 

Mithali Raj
Harmanpreet Kaur
Veda Krishnamurthy
Smriti Mandhana
Poonam Raut
Jhulan Goswami
Deepti Sharma


Iterating Arrays using standard for-loop: 

32
64
128
256
512

 

Java 1.5: Enhanced forEach loop

Syntax:

For Arrays:-

for (Object element : array) {
     System.out.println("Element: " + element);
}

For Collections:-

for (Object col : collection) {
      System.out.println("Collection: " + col);
}

IteratingUsingEnhancedForEachLoop.java

package in.bench.resources.various.forloop.types;

import java.util.ArrayList;
import java.util.List;

public class IteratingUsingEnhancedForEachLoop {

	public static void main(String[] args) {

		// invoke methods
		iteratingCollection();
		iteratingArrays();
	}

	public static void iteratingCollection() {

		// create collection to store player names
		List<String> players = new ArrayList<String>();

		// add player names to list
		players.add("Mithali Raj");
		players.add("Harmanpreet Kaur");
		players.add("Veda Krishnamurthy");
		players.add("Smriti Mandhana");
		players.add("Poonam Raut");
		players.add("Jhulan Goswami");
		players.add("Deepti Sharma");

		// iterate using enhanced forEach loop
		System.out.println("Iterating Collection using Java 1.5 enhanced forEach loop: \n");
		for(String player : players) {
			System.out.println(player);
		}
	}

	public static void iteratingArrays() {

		// create Integer Arrays
		Integer[] ramSize = new Integer[]{32, 64, 128, 256, 512};

		// iterate using enhanced forEach loop
		System.out.println("\n\nIterating Arrays using Java 1.5 enhanced forEach loop: \n");
		for(int ram : ramSize) {
			System.out.println(ram);
		}
	}
}

Output:

Iterating Collection using Java 1.5 enhanced forEach loop: 

Mithali Raj
Harmanpreet Kaur
Veda Krishnamurthy
Smriti Mandhana
Poonam Raut
Jhulan Goswami
Deepti Sharma


Iterating Arrays using Java 1.5 enhanced forEach loop: 

32
64
128
256
512

 

Java 1.8: Iterable’s forEach loop

Signature:

default void forEach(Consumer<? super T> action)

IteratingUsingForEachLoopInJava8.java

package in.bench.resources.various.forloop.types;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IteratingUsingForEachLoopInJava8 {

	public static void main(String[] args) {

		// invoke methods
		iteratingCollection();
		iteratingArrays();
	}

	public static void iteratingCollection() {

		// create collection to store player names
		List<String> players = new ArrayList<String>();

		// add player names to list
		players.add("Mithali Raj");
		players.add("Harmanpreet Kaur");
		players.add("Veda Krishnamurthy");
		players.add("Smriti Mandhana");
		players.add("Poonam Raut");
		players.add("Jhulan Goswami");
		players.add("Deepti Sharma");

		// iterate using Iterable's forEach loop
		System.out.println("Iterating Collection using Java 1.8 Iterable's forEach loop: \n");
		players.forEach(player -> System.out.println(player));
	}

	public static void iteratingArrays() {

		// create Integer Arrays
		Integer[] ramSize = new Integer[]{32, 64, 128, 256, 512};

		// Step 1: convert Arrays into List
		List<Integer> list = Arrays.asList(ramSize);

		// Step 2: iterate using Iterable's forEach loop
		System.out.println("\n\nIterating Arrays using Java 1.8 Iterable's forEach loop: \n");
		list.forEach(ram -> System.out.println(ram));
	}
}

Output:

Iterating Collection using Java 1.8 Iterable's forEach loop: 

Mithali Raj
Harmanpreet Kaur
Veda Krishnamurthy
Smriti Mandhana
Poonam Raut
Jhulan Goswami
Deepti Sharma


Iterating Arrays using Java 1.8 Iterable's forEach loop: 

32
64
128
256
512

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

To print first N natural numbers in Java