1. The most convenient loop for iterating the elements of arrays and collections.This loop has introduced in Java 1.5 version

    Example:

    class Test {
    
    	public static void main(String[] args) {
    		int[] a = { 10, 20, 30, 40, 50 }; // declare an array with 5 elements
    		// use for each to retrieve the elements from array
    		for (int x : a) {
    			System.out.println(x); // x represents each element of the array
    		}
    	}
    }
    /*
    Output:
    10
    20
    30
    40
    50
    */
    
  2. Program to display the elements of two dimensional int array by using for each loop.
     
    class Test {
    
    	public static void main(String[] args) {
    		int[][] a = { { 10, 20, 30 }, { 100, 200, 300 } };
    		for (int[] x : a) {
    			for (int b : x) {
    				System.out.println(b);
    			}
    		}
    	}
    }
    /*
    Output:
    10
    20
    30
    100
    200
    300
    */
    
  3. Using general for loop:
     
    class Test {
    
    	public static void main(String[] args) {
    		int[][] a = { { 10, 20, 30 }, { 100, 200, 300 } };
    		for (int i = 0; i < a.length; i++) {
    			for (int j = 0; j < a[i].length; j++) {
    				System.out.println(a[i][j]);
    			}
    		}
    	}
    }
    /*
    Output:
    10
    20
    30
    100
    200
    300
    */
    
  4. The main limitation of for each loop.It is applicable only for arrays and collections