All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Enhanced For Loop In Java

Last Updated : Mar 11, 2024

Enhanced For Loop In Java

In this article we will show you the solution of enhanced for loop in java, since we don't have access to the index in an enhanced for-loop, we are unable to replace elements at any particular index. The enhanced for loop will operate in a sequence way, always incrementing the counter by one.

This loop, which implements the iterable interface, is the only way we can iterate on that container.

We can only iterate in increments in this for-loop. Since we don't have access to the index in this for-loop, we are unable to change any specific index's worth of data.

Only the original order of the array elements, not the reverse, can be printed in the for-each loop.

Declarative The recently declared block variable has a type that is appropriate for the elements of the array you are accessing.

The variable will be accessible inside the for block, and its value will be identical to the element of the current array.

The array you need to loop through is the result of the expression given above. The expression could be a method call that returns an array or an array variable.

Any class that implements the Iterable interface, such as ArrayList, can use the extended for loop, sometimes known as a "for each" loop.

This version of the previous programme uses an improved for loop.

The enhanced loop operates sequentially. i.e., the counter is always increased by one, whereas in a for loop, the step can be changed to suit your needs.

For example, you could use i=i+2 to loop through every second element of an array or collection. Only incremental iterations are permitted in the improved for a loop.

It cannot be set up to go down in increments. For example, to move backward in a for loop, write i— in the step counter.

Step By Step Guide On Enhanced For Loop In Java :-

import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
 {
                   int[] array = { 20,40,60,80 };
                   for (int a : array) {
   System.out.println(a);
  }
 }
}
  1. In the first step, we will import all the packages by writing import.java.io*
  2. In The Second step, we will create a class GFG
  3. Thirdly, we will create a Main driver method i.e public static void main
  4. We will be initialising the array and write values 1,2,3,4,5 and 6 respectively.
  5. In the next step, we will access the value using for each loop
  6. Lastly, print all the elements in the array.
  7. Run the code

Conclusion :-

So, in this way we have implemented the enhanced for loop with an example in Java.

From the next tutorials, we will learn some more looping examples in Java.

I hope this article on enhanced for loop in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪