Computer Science, asked by fencermohit7393, 1 year ago

what is enhanced for loop in Java?

Answers

Answered by mayank3690
0

The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (Collections are not covered in these pages). It can also be used for arrays, as in the above example, but this is not the original purpose.

Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.

Two additional statement types, break and continue, can also control the behavior of enhanced for loops.

ADVANCED

The break and continue statements can be used with statement labels.

Answered by Anonymous
1

The enhanced for-loop has a syntax :

for ( datatype variable : array name )

{

Statements

}

This enhanced for-loop is a special type of for-loop which is used for the purpose of traversing each element in a particular array .

The array is a set of finite data which share the same datatype .

The loop is a part of a program that runs a set of instructions repeatedly .

Example of an enhanced for-loop :

int a[] = {1,2,3};

for ( int x : a )

{

System.out.println(x);

}

Output :

1

2

3

The enhanced for - loop should have a variable which has the same datatype as that of the array .

On each iteration of the loop , the array elements will get printed .

This is time consuming and easier to code .

Similar questions