Enhanced for loop in java with example

Enhanced for loop is simple version of for loop for java arrays


for(datatype variable : array_name)
body of loop

for(int i : value)

variable is required to hold element of array.
datatype is used for declaring variable data type.
array_name is used to defined which array is using enhanced for loop.


import java.util.Scanner;

class group{

public static void main(String arng[]){

int value[]= new int[5];

Scanner data = new Scanner(System.in);

float sum =0;

System.out.println("Enter 5 element of array" );

// Enhanced for loop

for(int i : value)

i = data.nextInt();

sum = sum+i;


System.out.println("Average of array element");
sum = sum/value.length;



Here value is array name 

Previous Code:-
Length function for array

List Code:-
Java codes

Next Code:-

Java Programs:-
List of Java Programs

One Comment

  1. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.

    java Training in Bangalore

    java training in chennai


Add your comments here

Back to Top