• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Enhanced For loop vs ForEach Loop

Hi,

what are differences. practical uses of Enhanced For loop vs ForEach Loop. When to use which one. Why one is called external iterator and other as internal iterator, please advise
0
gudii9
Asked:
gudii9
  • 2
2 Solutions
 
CPColinSenior Java ArchitectCommented:
In Java, the terms "enhanced for loop" and "foreach loop" refer to the same thing. Can you post a link to where you read that they were different?
0
 
krakatoaCommented:
for(int i=0;i<100;i++){
System.out.println(i);
}

Open in new window


... you are responsible for the halt condition.
int[] integers ={1,2,3};
for(int i: integers){System.out.println(i);
}

Open in new window

... Java handles the stop condition.
0
 
gudii9Author Commented:
i mean java 8 for each with lamda

https://www.mkyong.com/java8/java-8-foreach-examples/
0
 
CPColinSenior Java ArchitectCommented:
There are barely any differences between the two strategies, so use whichever one produces code that is easier for you to understand.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now