ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.