How to Write Enhanced For Loop in Java With Examples

In this tutorial, you will learn how to write enhanced for loop in JAVA 8. I will be showing you various examples with code in JAVA 8 to show how you can implement it in real-world scenarios.

What is Enhanced For Loop in JAVA?

The enhanced for loop[1] was first introduced in Java version 5. When traversing a collection of elements (including arrays), this is the most commonly utilized method.

Previously we used to write the for loop as shown in the below code. This was the traditional way to write a for loop to step through a list of elements.

for(int i=0; i < givenList.length; i++){
   // Your Logic Here
   // Some more lines of Code
}

Later after the introduction of Enhanced For Loop in JAVA, you can rewrite the above code in a simpler way and less code.

for(int x : givenLength){
   // Your Logic Here
   // Some more lines of Code
}
  • Declaration − In the above code, Declaration is int x, hence this declaration describes that the variable inside the for loop will be integer. Similar if you want JAVA to automatically set the declaration then you may use auto.
  • Expression − “: givenLength” is the expression that lets the for loop know from where to extract the elements or to iterate through which list.

Examples of Enhanced For Loop in JAVA

Let us see a few examples of Enhanced loops in JAVA, that you can use in your day-to-day programming assignments or projects.

public class Solution {
   public static void main(String args[]) {
      int [] givenNumbers = {1, 2, 3, 4, 5};

      //Initializing Enhanced For Loop
      for(int x: givenNumbers){
        System.out.print(x);
        System.out.print(" ");
      }
   }
}

Output:

1 2 3 4 5

In the above example code, we are looping through or iterating through givenNumbers array and printing them one by one. As explained the variable inside the block is x and it is an integer. In the next example, we will see how you can use the same code to print a string array.

public class Solution {

   public static void main(String args[]) {
      String [] givenWords = {"You", "Are", "On", "Coduber", "Website."};

      //Initializing Enhanced For Loop
      for(String Word: givenWords){
        System.out.print(Word);
        System.out.print(" ");
      }
   }
}

Output:

You Are On Coduber Website.
How to Write Enhanced For Loop in Java With Examples

As you saw in the above code as well, an enhanced loop was used to iterate through givenWords to print each word present in the list. The variable inside the block is a string.

Limitation of Enhanced Loop in JAVA

Enhanced for loops are straightforward, but they are restrictive. They can be used when you want to iterate through the items of an array in first-to-last order and you don’t need to know the index of the current element in order to accomplish this. In all other circumstances, the “normal” for loop should be used instead of the alternative.

Wrap Up

That is all for using the latest version of For Loop in JAVA. You learned in this post how to write and use enhanced for loop. Also, we told you what is the limitation of using such for loop and when you should and when you should not use it.

Let me know in the comment section if you think that you can suggest a better tutorial or improvement in the above post. Or you have or know any code that is much faster than the methods mentioned above.

Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. JAVA: Meaning of \n and \t With Examples
  2. Parse (split) or Tokenize a String in C++ Using Delimiter

Leave a Comment