Difference Between Forloop and Enchance For Loop
Difference Between Forloop and Enchance For Loop
---------------------
The traditional for loop was introduced in the first release of the Java language.
System.out.print("Get set...");
for(int i = 1; i < 4; i++) {
System.out.print(i + "...");
}
we start the loop index variable i at 1 and limit it to a value of less than 4.
Note that the for loop requires us to tell the loop to increment.
In this case we’ve also elected a pre- versus post-increment.
pros:
-----
1->The traditional for loop uses a counter and allows you to iterate until the last
element is reached
i.e. counter is equal to the length of the array.
2->for loop, i which we defined is a single variable that is mutated through each
iteration of the loop.
3->for loop provide access to the index, hence allows you to replace any element in
the array.
cons:
-----
1->The for loop is quite capable, but it has too many moving parts.
2->In order to loop over a container we must implement the Iterable interface, both
array and collection implement
but there is no such requirement for traditional for loop.
pros:
-----
2) The enhanced for loop executes in sequence. i.e the counter is always increased
by one,
where as in for loop you can change the step as per your wish e.g doing something
like i=i+2; to loop every second element in an array or collection.
3) The enhanced for loop can only iterate in incremental order. we cannot configure
it to go in decrement.
i.e in for loop we can write i-- in step counter to go backward.
4)If we have a requirement that array should be displayed sequence in the forward
direction and
we also don't want to change the real value in the array accidently or
intentionally by any user etc then we should use the enhanced for loop.
cons:
-----
1->To remove elements as you traverse collections.
2->To modify the current slot in an array or list.
3->To iterate over multiple collections or arrays.
its working:
------------
The iteration variable in the for-each loop receives every element of an array or
collection one at a time starting from first element to last element.
i.e In the first iteration, it gets the first element. In the second iteration,
it gets the second element and so on. Thus it iterates all elements of an array or
the collection.
The type of iteration variable must be compatible with the type of array or
collection.
pros:
-----
1->You need not to specify the initialization, condition and increment or decrement
as you specify in the normal for loop.
2->It increases the readability of the code.
cons:
-----
You can’t iterate only few elements of an array or collection using for-each loop.