Java Tutorial : For-Each Loop
Syntax:
for(val : collection) { statements; }
Here , the var is attached to the collections.This var represents each element of the collection one by one.
Example : here is a program to see the use of for-each loop and retrieve the elements one by one from an array and display it.
// using for each loop to display array elementsclass demo {
public static void main(String[] args) { //Declare an array with 4 elements
int arr[] = { 210, 540, 230, -23 };
// Use of for-each Loop
for (int i : arr) { System.out.println(i); } }}
Leave a Comment