10 Difference Between For And For –Each Loop In Java

SHARE

What Is For Loop?

A For-loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly. A for-loop has two parts: a header specifying the iteration and a body which is executed once per iteration. The header often declares an explicit loop counter or loop variable, which allows the body to know which iteration is being executed. For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.

What You Need To Know About For Loop

  • The for loop is a control structure for specifying iteration that allow code to be repeatedly executed.
  • It uses an index of an element to fetch data from an array.
  • Termination of this loop is controlled by loop condition. Therefore, For loop executes repeatedly until the given condition turns out to be false. 
  • It requires loop counter, initial and end values, in order to iterate through all the elements of an array.
  • It was introduced in Java 1.
  • It works on indexable objects where random access of elements is allowed.
  • When we iterate through using traditional for loop we can manipulate the actual data of an array.
  • The For loop is harder to read and write than the Foreach loop.
  • A for loop can be used to retrieve a particular set of elements.

What Is For-Each Loop?

Foreach loop  is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say “do this to everything in this set”, rather than “do this x times”. This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.

The foreach statement in some languages has some defined order, processing each item in the collection from the first to the last. The foreach statement in many other languages, especially array programming languages, does not have any particular order. This simplifies loop optimization in general and in particular allows vector processing of items in the collection concurrently.

What You Need To Know About For-Each Loop

  • The foreach loop is a control structure for traversing items in an array or a collection.
  • It uses an iteration variable to automatically fetch data from an array.
  • Break statement can be used to terminate the execution of the loop early, otherwise, it executes until the last element gets evaluated.
  • It automates the iteration by using the iteration variable which stores one element each time.
  • It was introduced back in Java 5.
  • It works on all indexable objects even when random access of an individual element is not allowed.
  • The iteration variable in for-reach is read-only. It means changes made on iteration value would not affect the actual data of the collection or array.
  • The Foreach loop is easier to read and write than the for loop.
  • The Foreach loop cannot be used to retrieve a particular set of elements.

Difference Between For And For –Each Loop In Tabular Form

BASIS OF COMPARISON FOR LOOP FOR-EACH LOOP
Description The for loop is a control structure for specifying iteration that allow code to be repeatedly executed.   The foreach loop is a control structure for traversing items in an array or a collection.  
Working It uses an index of an element to fetch data from an array.   It uses an iteration variable to automatically fetch data from an array.  
Execution Termination of this loop is controlled by loop condition. Therefore, For loop executes repeatedly until the given condition turns out to be false.    Break statement can be used to terminate the execution of the loop early, otherwise, it executes until the last element gets evaluated.  
Iteration It requires loop counter, initial and end values, in order to iterate through all the elements of an array.   It automates the iteration by using the iteration variable which stores one element each time.  
Introduction It was introduced in Java 1.   It was introduced back in Java 5.  
Indexable Objects It works on indexable objects where random access of elements is allowed.   It works on all indexable objects even when random access of an individual element is not allowed.  
Manipulation Of Array When we iterate through using traditional for loop we can manipulate the actual data of an array.   The iteration variable in for-reach is read-only. It means changes made on iteration value would not affect the actual data of the collection or array.  
Reading 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.  
Use 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.