Difference Between for Loop and foreach Loop

Both for loop and foreach loop are control structures that are used to repeat a block of statements. There are repetition control structures in programming to execute a block of statements again and again. One common control structure is for a loop. A for loop is a control flow structure used for iteration that allows code to be repeatedly executed. It contains the initialization, test expression and the update expression. The statements to repeat is included within the curly braces. The foreach loop is improved for a loop. It increases the code readability, and it is easy to write. Both for loop and foreach loop are used to repeat a set of statements, but the syntax is different. The key difference between for Loop and foreach loop is that the for loop is a general purpose control structure while the foreach loop is an enhanced for loop that is applicable only to arrays and collections.

CONTENTS

1. Overview and Key Difference
2. What is for Loop
3. What is foreach Loop
4. Similarities Between for Loop and foreach Loop
5. Side by Side Comparison – for Loop vs foreach Loop in Tabular Form
6. Summary

What is for Loop?

The for loop is a common repetition structure. It helps to iterate through a statement or a set of statements in the program. The syntax of the for loop is as follows.

for(initialization; test expression; update expression){

// code inside the for loop

}

The initialization occurs first. Then the test expression is checked. If the evaluated answer is true, the code inside the for loop executes. At the  end of the last statement of the  for loop, the update expression is evaluated. Then the test expression is evaluated again. If it is true, the code inside the for loop executes. At the end of the for loop, the update expression is evaluated again and checked with the test expression. This process repeats until the test expression becomes false. When it becomes false, the for loop terminates.

Figure 01: Program with for loop and foreach loop

According to the above program, the array1 can store multiple elements of type integer. In the for loop, the i is 0. It is less than 5. So, the 0th index element of the array1 is printed. It is number 10. Then the i is incremented due to the update expression. Now the i value is 1. It is less than 5. So, the 1th  index element of the array1 is printed. Again the i is incremented. This process continues.  When the i value becomes 5, the test expression is false because it is not less than 5. So, the loop terminates.

What is foreach Loop?

The foreach loop is a convenient way to retrieve elements of an array or a collection. It can be used as an alternative to the for a loop. It is referred as the foreach loop because it iterates through each element of the array or the collection. The syntax of the foreach loop is as follows.

for(data type  item: collection){

//code inside the for each loop

}

The collection is the array or the collection that should be iterated through. The item is the single element from the collection. The foreach loop iterates through each element and stores that element in the variable item. Then executes the statements inside the foreach loop.

According to the above program, the array1 stores multiple integers. The enhanced for loop is used to iterate through the elements of the array. Each element is stored to the variable i and the code inside the foreach loop executes. The foreach loop achieves the same tasks as the  for loop, but it is more readable and easy to write. Therefore, it is known as ‘enhanced for loop‘.

What is the Similarity Between for Loop and foreach Loop?

  • Both for loop and foreach loop can be used to executes a set of statements multiple times.

What is the Difference Between for loop and foreach Loop?

for Loop vs foreach Loop

The for loop is a control structure for specifying iteration that allows code to be repeatedly executed. The foreach loop is a control structure for traversing items in an array or a collection.
 Element Retrieving
A for loop can be used to retrieve a particular set of elements. The foreach loop cannot be used to retrieve a particular set of elements.
Readability
The for loop is harder to read and write than the foreach loop. The foreach loop is easier to read and write than the for loop.
 Usage
The for loop is used as a general purpose loop. The foreach loop is used for arrays and collections.

Summary – for Loop vs foreach Loop

In programming, sometimes it is required to repeat the code. The for loop is used commonly to achieve this task. A for loop is a control flow structure used for iteration that allows code to be repeatedly executed. The foreach loop is improved for loop that is easy to read and write. The difference between for Loop and foreach loop is that the for loop is a general purpose control structure while the foreach loop is an enhanced for loop that is applicable only to arrays and collections.

Reference:

1.Programiz, Java for-Each Loop (Enhanced for Loop). Available here