Java for each loop example.


IncludeHelp 22 June 2016

Java introduced a new loop style that is for each loop with the help of this loop we can traverse array or collection elements, it will be much easier and bug chances are very less.

Java Code Snippet - Example of for each loop

Syntax

for( data_type variable_name : array or collection_variable){
     body of the loop;
}

Example

//Example of for each loop in java
public class ExForEachLoop{
	
	  public static void main(String args[]){
		  String studentNames[]={"Mike", "Rocky", "Monti", "Charlse"};
		  
		  //printing student names
		  for(String sn:studentNames){
			  System.out.println(sn);
		  }
	  }
}
    

    Mike
    Rocky
    Monti
    Charlse



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.