-
Notifications
You must be signed in to change notification settings - Fork 0
Java Loops Types For Each
Also called the enhanced for loop, it is an extremely useful and simple way to iterate over each item in a collection, arrays as well as objects that implement the Iterable interface.
for (object : iterable)
{
// Statements
}
The loop is read as - for each element in the iterable
(could be an array, collectable etc.). The object
type must match the element type of the iterable
.
int[] number_list = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int numbers : number_list)
{
System.out.print (numbers + " ");
//Iterated 10 times, numbers 0,1,2...9
}
Output:
0 1 2 3 4 5 6 7 8 9
🚀 Run Code
In the above code snippet, number_list
is an array. If you don't know what this is, don't worry about it. An array is a container object that holds a fixed number of values of a single type, but more on this later.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links