for loop is a basic operation in programming languages, in Java, the regular for loop got an introduction in the initial version and the feature is still getting continued.
So in this case why the for each get introduced in subsequent versions and what is the main reason behind it.
Let us check it below
The main differences we can identify between for loop and for-each (Enhanced for loop) are as follows
- No Element size verification while initializing whereas regular for loop has expressing size define explicitly.
- No Incremental/Decrement operator is required in the initialization for each(Enhanced for-loop).
We have provided detailed differences between for loop and for-each(enhanced for loop) with examples
Common Term | For Loop | For Each (Enhanced Loop) |
Supported Version | Java 1 | Java 1.5 |
Performance | High compared with forEach | Less compared with For Loop |
Looping Logic | Looping through Array Based, Incremental | Looping will happen on array based, Will not support incremental Support |
Operations | Supports Break, Continue | Supports Break, Continue |
Code Efficient | Harder Compared with for each | Better than for regular loop, Code Initialization Required |
Loop Index | Loop index can tracked because the size and incremental logic in defined explicitly | Loop index can not be tracked because the size and incremental logic not explicitly and size will be assigned to the variable indirectly. |
Reliable Code | This is less reliable than for each, there might a chance there can a exception happen inside the loop. | for each is more reliable in terms of error-prone free code, because there will not be much user interaction required inside the loop, this omits lots of exceptions during coding |
Syntax | for(variable_initialization; size_check_expression; flow_operator) { } | for(variable_typ_declarion : source_object) { } |
Examples to show the difference between regular For Loop and for Each Enhanced Loop :
List<String> loopString = Arrays.asList("A","B","C","D","E"); for (int i=0;i<loopString.size();i++) { System.out.println("For Loop list Iteration : "+loopString.get(i)); } int loopInt = 10; for (int i=0;i<loopInt;i++) { System.out.println("For Loop Integer Iteration : "+i); } int loopBreak = 20; for (int i=0;i<loopBreak;i++) { if( i<5) continue; System.out.println("For Loop Integer Break, Continue Iteration : "+i); if( i>=15) break; }
Output :
For Loop list Iteration : A For Loop list Iteration : B For Loop list Iteration : C For Loop list Iteration : D For Loop list Iteration : E For Loop Integer Iteration : 0 For Loop Integer Iteration : 1 For Loop Integer Iteration : 2 For Loop Integer Iteration : 3 For Loop Integer Iteration : 4 For Loop Integer Iteration : 5 For Loop Integer Iteration : 6 For Loop Integer Iteration : 7 For Loop Integer Iteration : 8 For Loop Integer Iteration : 9 For Loop Integer Break, Continue Iteration : 5 For Loop Integer Break, Continue Iteration : 6 For Loop Integer Break, Continue Iteration : 7 For Loop Integer Break, Continue Iteration : 8 For Loop Integer Break, Continue Iteration : 9 For Loop Integer Break, Continue Iteration : 10 For Loop Integer Break, Continue Iteration : 11 For Loop Integer Break, Continue Iteration : 12 For Loop Integer Break, Continue Iteration : 13 For Loop Integer Break, Continue Iteration : 14 For Loop Integer Break, Continue Iteration : 15
Below example shows how to use the for each in java
List<String> loopEachString = Arrays.asList("A","B","C","D","E"); for(String loopEachStringIter : loopEachString) { System.out.println("For Each Loop List : "+loopEachStringIter); } // Below part wont work, Since foreach it array based itereation /*int loopEachInt = 10; for(int loopEachIntIter : loopEachInt) { System.out.println("For Loop Integer Iteration : "+loopEachIntIter); }*/ List<String> loopEachBreakString = new ArrayList<String>(); loopEachBreakString.add("A"); loopEachBreakString.add("B"); loopEachBreakString.add("C"); loopEachBreakString.add("D"); loopEachBreakString.add("E"); for(String loopEachStringIter : loopEachBreakString) { if (loopEachStringIter.equals("C")) continue; System.out.println("For Each Loop Break, Continue : "+loopEachStringIter); if (loopEachStringIter.equals("D")) break; }
Output :
For Each Loop List : A For Each Loop List : B For Each Loop List : C For Each Loop List : D For Each Loop List : E For Each Loop Break, Continue : A For Each Loop Break, Continue : B For Each Loop Break, Continue : D
Regular For Loop doe support data modifications while iteration whereas the for each does not support the modification. It throws modification error, below example explain this.
// Modification inside the loop List<String> loopModifyString = new ArrayList<String>(); loopModifyString.add("A"); loopModifyString.add("B"); loopModifyString.add("C"); loopModifyString.add("D"); loopModifyString.add("E"); for(int i=0;i<loopModifyString.size();i++) { System.out.println("Forloop Modifications : "+loopModifyString); loopModifyString.remove(1); //This Works } List<String> loopEachModifykString = new ArrayList<String>(); loopEachModifykString.add("A"); loopEachModifykString.add("B"); loopEachModifykString.add("C"); loopEachModifykString.add("D"); loopEachModifykString.add("E"); for(String loopEachStringIter : loopEachModifykString) { System.out.println("For Each Modifications : "+loopEachStringIter); loopEachModifykString.remove(1); //This will return error } loopEachModifykString.add(1, null);//This works
Output of above Program :
Forloop Modifications : [A, C, D, E] Forloop Modifications : [A, D, E] For Each Modifications : A Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at stringdate.ForLoopForEach.main(ForLoopForEach.java:117)
In above example, for each returns error in modification.
Things to note the limitations of regular for and enhanced for loop :
– Regular for loop provides the features to modify the collections which we are trying to loop through, It is Error-prone due to the options provided, Provides plenty of features.
– Enhanced for loop has limited features but works well what is expected with limited features.