Sunday, 18 August 2013

How does Foreach statement in Java work ?

Java Language does not support the foreach statement like in C#.

Whereas, there is another way in Java through which you can have he same functionality. This is the modified form of the for statement and is known as the "Enhanced for Statement."

Enhanced for statement

To iterate through Collections and Arrays, the enhanced for statement can be used to make loops more compact and easy to read.

Code Example:

class EnhancedForDemo {

    public static void main(String[] args){

         int[] numbers =


         for (int item : numbers) {

             System.out.println("Count is: " + item);



See Also

Control Flow Statements (Tutorial) - Java SE

Arrays Tutorial - DOTNET

Get Ebooks delivered to your email id


Subscribe to our channel


Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists