How Does The Java 'For Each' Loop Work

Introduction

In this blog, you will learn about how the 'for each' loop works. The 'for each' loop work similarly to another loop but it can not traverse the array or collection element in reverse order.

Example

import java.util.*;

class ForEachLoopExample {

	public static void main(String args[]){

		String arr[]={
			"Abhishek",
			"Harshit",
			"Ishika",
			"Shanu",
			"Swarnim",
			"Avish",
			"Avin",
			"Shruti",
			"Pranav"
		};
        
		// for each loop 
	    for(String str:arr){
		  System.out.println(str+" ");
		}
		
	}
}

Output

How does the Java 'for each' loop work? 

Conclusion

In this blog, we have seen how the 'for each' loop work in java. Thanks for reading and I hope you like it. If you have any suggestions or queries about this blog, please share your thoughts. You can read my other blog by clicking here.

Happy learning, friends!

Next Recommended Reading Two Sum Array Problem In Java