JAVA: Enhanced For Loop


Enhanced for loop was introduced in JAVA 5 as a simple way to iterate all the elements of a collection.

It is useful when scanning a array instead of using for loop. 

Here we will take a simple example to sum all the elements of an array


    public class EnhancedForLoop {

  
    public static void main(String[] args) {
       int  arr[]={3,2,4,5};
       int sum=0;
       
for(int x:arr){
       sum+=x;}
    System.out.print(sum);
    }
    }

You can also watch my video on YouTube on the same topic. Java:Enhanced For Loop
In Association with Amazon.in In Association with Amazon.in

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily