for-each loop in Java





  • The for statement also has another form designed for iteration through Collections and arrays .

  • This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.

  • It works on elements basis not index. It returns element one by one in the defined variable.

Syntax

for(Type var:array){  
//code to be executed  
}  
To demonstrate, consider the following array, which holds the numbers 1 through 10:
class EnhancedForDemo
{
 public static void main(String[] arg){
 int[] numbers ={1,2,3,4,5,6,7,8,9,10};
 for (int item : numbers) {
  System.out.println("Count is: " + item);
  }
 }
}      
OUTPUT:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

Next topic is Java-break-continue




 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved