☕ Java
Beginner
What is the enhanced for loop (for-each) in Java?
Answer
The enhanced for loop (also called for-each) provides a cleaner syntax for iterating over arrays and any class that implements the Iterable interface: for (String name : names) { System.out.println(name); }. It eliminates the need for index management and is less error-prone than traditional index-based loops. Drawbacks: you cannot modify the underlying collection while iterating (throws ConcurrentModificationException), you cannot access the current index, and you cannot iterate in reverse. For these cases, use a traditional for loop, an Iterator, or Java 8 streams with forEach.