Javatpoint Logo
Javatpoint Logo

For-each loop (Advanced or Enhanced For loop):

The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.

Advantage of for-each loop:

  • It makes the code more readable.
  • It elimnates the possibility of programming errors.

Syntax of for-each loop:


Simple Example of for-each loop for traversing the array elements:

Test it Now
Output:12
       13
       14
       44

Simple Example of for-each loop for traversing the collection elements:

Test it Now
Output:vimal
       sonoo
       ratan
Next TopicVarargs




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials