A loop is a control statement which executes a particular block of code repeatedly until a given condition becomes false. There are various types of loops such as while, do-while and for loop.
In this article, we will be focusing on for loop and its enhanced version.
If you’re looking for forEach method introduced in Java 8: ForEach Method in Java 8
For Loop | Foreach Loop |
---|---|
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 |
Termination of this traditional loop is controlled by loop condition. Therefore, For loop executes repeatedly until the given condition turns out to be false | No such provision is provided in foreach, instead break statement can be used to terminate the execution of the loop early, otherwise, it executes until the last element gets evaluated |
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 |
When we iterate through using traditional for loop we can manipulate the actual data of an array | The iteration variable in foreach is read-only. It means changes made on iteration value would not affect the actual data of the collection or array |
It was introduced in Java 1 | It was introduced back in Java 5 |
It has the flexibility of the iterating an array whether it is increasing order or decreasing order | It can iterate only in a linear manner from initial to end element, but not vice versa |
Syntax: for( initialization; condition; iteration) { // body } | Syntax: for(type var_name: collection) { // body } |
It works only on indexable objects where random access of elements are allowed | It works on all indexable objects even when random access of an individual element is not allowed |
It is generally known as a traditional for loop.
In Java, “for loop” is used to execute a particular block of code a fixed number of times. In other words, when the number of iterations are already known, then it is typically used.
It provides compact looping structure as initialization, condition and iteration are put together within a single statement.
Like C++, a simple for loop comprises three parts, initialization, condition and increment/decrement
1. Initialization: In this, a counter or control variable is assigned with some value and acts as a base for the loop counter
2. Condition: It is a relational or logical expression that returns a boolean value, true or false. It is evaluated after each iteration and continues execution until the condition turns out to be false.
3. Increment/ decrement: It updates with each iteration with incremented or decremented values
.
Syntax :
for(initialization;condition; updatableExp){ //loop body }
For each is the enhanced for loop which was introduced in Java 5. It is used to iterate through a collection of objects.
Unlike traditional for loop, the enhanced loop isn’t declared with control variable, condition, and increment/decrement, instead, an element of the same array or list type needs to be declared with a colon, is followed by the list or array name.
It executes until all the elements in the list are traversed.
Syntax:
for (type element : list/array) { // loop body }
Program:
public class ForLoopExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList(); list.add(5); list.add(9); list.add(7); list.add(65); list.add(4); list.add(0); for(int i=0;i<list.size();i++) { System.out.println("List items : "+list.get(i)); } } }
Output:
List items : 5 List items : 9 List items : 7 List items : 65 List items : 4 List items : 0
public class ForLoopExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList(); list.add(5); list.add(9); list.add(7); list.add(65); list.add(4); list.add(0); for (Integer value: list ) { System.out.println("Items : "+value); } } }
Output:
Items : 5 Items : 9 Items : 7 Items : 65 Items : 4 Items : 0
This post was last modified on September 24, 2022