In enhanced for loop how does the loop knows how to terminate? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In enhanced for loop how does the loop knows how to terminate?

Enhanced For Loop

10th Jan 2017, 5:57 AM
Nabajyoti Kumar Das
Nabajyoti Kumar Das - avatar
3 Answers
+ 3
Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred. Two additional statement types, break and continue, can also control the behavior of enhanced for loops.
10th Jan 2017, 8:47 AM
Gaurav Singh
Gaurav Singh - avatar
+ 2
If you compare the enhanced for loop to the typical way of iterating over a collection, it's clear that the for each loop is simpler and can make your code more readable. Also note that the enhanced for loop is designed to simplify your work with generics. Although this tip does include two examples of using the enhanced for loop with generics, it's not the focus of the tip. Instead the objective of this tip is to introduces you to the more basic changes you can make to your code to use the for each loop. It's important to note that the enhanced for loop can't be used everywhere. You can't use the enhanced for loop: To remove elements as you traverse collectionsTo modify the current slot in an array or listTo iterate over multiple collections or arrays
11th Jan 2017, 6:35 PM
Aman kumar Jain
Aman kumar Jain - avatar
+ 1
by reaching last element in collection...
10th Jan 2017, 6:19 AM
Masoud Farmani
Masoud Farmani - avatar