Please enable JavaScript to use CodeHS

CodeHS Glossary


For Each Loop Java

A for loop that is written differently so that it loops through each element in a data structure, as opposed to having a loop counter variable that goes from 0 to length-1. Say you have an `ArrayList` called `list`. You can loop through each element in the list with a **for each** loop like so: ``` for(String elem: list) { // use elem here in the body System.out.println(elem); } ``` As opposed to the traditional for loop: ``` for(int i = 0; i < list.size(); i++) { String elem = list.get(i); System.out.println(elem); } ```