Each programming language makes use of loops to execute the identical block of code iteratively. Java has various kinds of loop statements, viz. for loop, whereas loop, do-while loop. One other sort of loop launched within the Java 5.0 model is the for-each loop, additionally referred to as enhanced for loop.

It makes use of the identical key phrase ‘for’ as in for loop to iterate in accumulating objects, corresponding to an array. In a for-each loop, there is no such thing as a have to initialize the loop counter variable. As a substitute, a variable is said together with the array identify. To get extra understanding of its utilization, test the syntax of the for-each loop in Java.

Syntax of For-Every Loop in Java

for(data_type variable_name : array_name) {

           Statements;

}

Right here, data_type is the information sort of the variable declared with the identify variable_name. array_name is the array through which the variable will iterate to execute the statements contained in the loop block.

How Does For-Every Loop Work?

For-each loop in Java works like another loop. The loop traverses for every component of the array until final. The worth of the array component is saved within the variable declared with the loop, and execution of the assertion happens for every iteration.

Learn: Java Developer Wage in India

Examples of the For-Every Loop in Java

Instance 1: Think about the beneath instance that provides the weather of the array and prints the consequence.

Class Example1 {

Public static void predominant(String args[]) {

int rely[] = {1, 3, 5, 7, 9};

int sum=0;

for(int var : rely) {

                            sum = sum + var;

}

System.out.println(“Sum of the array parts “ + sum);

}

}

Output: Sum of the array parts 25

Rationalization: Within the above program, for every iteration, the array component will get assigned to var and will get added to the variable referred to as sum.

For the primary iteration, var = 1, and the sum, which was initially 0, will get added to var, i.e., 1. Due to this fact, the sum turns into 1 after the primary iteration.

For second iteration, var = 3 and sum = sum + var = 1 + 3 = 4

For the third iteration, var = 5 and sum = 4 + 5 = 9.

On this manner, the sum will get up to date after every iteration and provides the sum of all parts of the array.

Instance 2: The beneath code snippet prints the weather of the string assortment.

Class Example2 {

Public static void predominant(String args[]) {

String firstName[] = {“Peter”, “John”, “Mary”};

for(int identify : firstName) {

                            System.out.println(“Title is “ + identify);

}

}

}

Output:

Peter

John

Mary

Rationalization: On this instance, the variable declared because the identify takes the worth of firstName from the gathering of strings and prints it.

How is the For-Every Loop Completely different from For Loop?

If we write the above code utilizing for loop, it will likely be as beneath:

Class Example1 {

Public static void predominant(String args[]) {

int rely[] = {1, 3, 5, 7, 9};

int sum=0;

for(int var=0; var < rely.size; var++) {

                            sum = sum + var;

}

System.out.println(“Sum of the array parts “ + sum);

}

}

By evaluating the code of for loop and for-each loop, it’s clear that it’s simple to write down the code utilizing the for-each loop. There isn’t any have to initialize the counter variable and increment or decrement within the for-each loop because the loop robotically strikes to the following component within the array.

Benefits of For-Every Loop in Java

  • Using the for-each loop is simple and makes the code readable.
  • The for-each loop doesn’t use the index of the array to traverse within the loop.
  • This loop reduces the possibility of programming error.
  • The for-each loop works quicker than the for loop.

Disadvantages of For-Every Loop in Java

There are particular disadvantages of utilizing the for-each loop as mentioned beneath:

  • It’s unattainable to traverse within the reverse order within the for-each loop as is completed in for loop or whereas by decrementing the counter variable.
  • There isn’t any option to skip the array component whereas utilizing the for-each loop.
  • It’s unattainable to seek advice from the odd and even parts within the array as doable in for loop the place the counter variable may be incremented or decremented by two or much more.

Checkout: Java Challenge Concepts & Subjects

Conclusion

Using the for-each loop in Java makes the code extra readable and simple to know. Due to this fact, it is suggested to make use of the for-each loop as a substitute of for loop. Nevertheless, it has the drawback that parts can’t be traversed in reverse order or can not skip the array parts. However using ‘break’ and ‘continue’ can let the coders modify the code as per the necessity. Java is a well-liked language for software program improvement. You’ll be able to study software program improvement from upGrad by making use of for Masters in Pc Science.

If you happen to’re to study extra about PHP, Full-stack software program improvement, take a look at upGrad & IIIT-B’s PG Diploma in Full-stack Software program Growth which is designed for working professionals and presents 500+ hours of rigorous coaching, 9+ initiatives, and assignments, IIIT-B Alumni standing, sensible hands-on capstone initiatives & job help with prime corporations.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE’S PG DIPLOMA IN FULL STACK

ENROLL TODAY

About the Author

Socially Keeda

Socially Keeda, the pioneer of news sources in India operates under the philosophy of keeping its readers informed. SociallyKeeda.com tells the story of India and it offers fresh, compelling content that’s useful and informative for its readers.

View All Articles