Enhanced for loop in java with example

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

Syntax:-

for(datatype variable : array_name)
{
body of loop
}

Example:-
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.

Example:-

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;
System.out.println(sum);

}

}

Here value is array name 




Previous Code:-
Length function for array

List Code:-
Java codes

Next Code:-


Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top