How to create a loop counter for a for each (#21512)
This commit is contained in:
@ -56,6 +56,8 @@ Both the above pieces of code snippets do the same work , however , clearly, the
|
|||||||
With the enhanced for loops we no longer need to mention starting and ending points for the loop,thus reducing OutofBounds errors.
|
With the enhanced for loops we no longer need to mention starting and ending points for the loop,thus reducing OutofBounds errors.
|
||||||
The need for loop counters and manual indexing are removed, and readability of the code is improved.
|
The need for loop counters and manual indexing are removed, and readability of the code is improved.
|
||||||
|
|
||||||
|
If we do want a loop counter for a for each loop, all we have to do is create a variable outside the loop and iterate within it.
|
||||||
|
|
||||||
It is important to note that making changes to the iterating variable for enhanced for loops within the loop causes no changes to the original collection elements.
|
It is important to note that making changes to the iterating variable for enhanced for loops within the loop causes no changes to the original collection elements.
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user