How does a for each loop guard against an empty list?

My question is how does a for each loop work for an empty list

ForEach also works in the same way. If the length is zero then loop is never executed.

The only difference between them is use ForEach loop when you want to iterate all the items of the list or array whereas in case of normal for loop you can control start and end index.


It uses the iterator of the Iterable collection, e.g. List. It is the duty of the implementer of the Iterator to write the hasnext() method to return false if there is no next item which will be the case if the collection is empty