Docy

For-Each Loop

Estimated reading: 1 minute 841 views

For-Each Loop

There is also a “for-each” loop, which is used exclusively to loop through elements in an array:

Syntax

				
					for (type variableName : arrayName) {
  // code block to be executed
}
				
			

The following example outputs all elements in the cars array, using a “for-each” loop:

 
				
					String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
  System.out.println(i);
}
				
			

Leave a Comment

Share this Doc
CONTENTS