0% found this document useful (0 votes)
35 views2 pages

7-Jagged Array and Enhanced Forloop-21-Jul-2020Material I 21-Jul-2020 Lecture4.1enhanced Forloop

The Java for-each loop, also known as the enhanced for loop, allows traversing array or collection elements in a simple and readable way. It eliminates the possibility of bugs from improper indexing. While it can only iterate in the forward direction and cannot skip elements, it is recommended over traditional for loops for readability when the intent is to iterate through each element. The syntax uses a data type and variable followed by a colon and the array or collection. It stores each element in the variable and executes the loop body before moving to the next element.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

7-Jagged Array and Enhanced Forloop-21-Jul-2020Material I 21-Jul-2020 Lecture4.1enhanced Forloop

The Java for-each loop, also known as the enhanced for loop, allows traversing array or collection elements in a simple and readable way. It eliminates the possibility of bugs from improper indexing. While it can only iterate in the forward direction and cannot skip elements, it is recommended over traditional for loops for readability when the intent is to iterate through each element. The syntax uses a data type and variable followed by a colon and the array or collection. It stores each element in the variable and executes the loop body before moving to the next element.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java For-each Loop | Enhanced For Loop

The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. It provides an
alternative approach to traverse the array or collection in Java. It is mainly used to
traverse the array or collection elements. The advantage of the for-each loop is that it
eliminates the possibility of bugs and makes the code more readable. It is known as the
for-each loop because it traverses each element one by one.

The drawback of the enhanced for loop is that it cannot traverse the elements in reverse
order. Here, you do not have the option to skip any element because it does not work on
an index basis. Moreover, you cannot traverse the odd or even elements only.

But, it is recommended to use the Java for-each loop for traversing the elements of
array and collection because it makes the code readable.

Advantages
o It makes the code more readable.
o It eliminates the possibility of programming errors.

Syntax
The syntax of Java for-each loop consists of data_type with the variable followed by a
colon (:), then array or collection.

1. for(data_type variable : array | collection){  
2. //body of for-each loop  
3. }  

How it works?
The Java for-each loop traverses the array or collection until the last element. For each
element, it stores the element in the variable and executes the body of the for-each
loop.

For-each loop Example: Traversing the array elements


1. //An example of Java for-each loop  
2. class ForEachExample1{  
3.   public static void main(String args[]){  
4.    //declaring an array  
5.    int arr[]={12,13,14,44};  
6.    //traversing the array with for-each loop  
7.    for(int i:arr){  
8.      System.out.println(i);  
9.    }  
10.  }   
11. }  
12.       
Test it Now

Output:

12
12
14
44

Let us see another of Java for-each loop where we are going to total the elements.

1. class ForEachExample1{  
2.   public static void main(String args[]){  
3.    int arr[]={12,13,14,44};  
4.    int total=0;  
5.    for(int i:arr){  
6.      total=total+i;  
7.    }  
8.   System.out.println("Total: "+total);  
9.  }   
10. }  

Output:

Total: 83

You might also like