Java Reference
In-Depth Information
Using the while statement...
1
2
3
Using the do-while statement...
1
2
3
Using the for statement...
1
2
3
The
for..in
statement is used to iterate over indices of arrays or property names of
objects. It can be used with collections such as arrays, lists, maps, any Nashorn objects,
and so on. Its syntax is:
for(var index in object)
Statement;
First, the
object
is evaluated. If it evaluates to
null
or
undefined
, the entire
for..
in
statement is skipped. If it evaluates to an object, the statement assigns the enumerable
property to the
index
and executes the body. The index is of the string type. In case
of an array, the
index
is the index of the array element as a string. In case of any other
collections such as a list or a map (a Nashorn object is also a map), the property of the
object is assigned to
index
. You can use the bracket notation (
object[index]
) to access
the value of the property. The following code demonstrates how to use the
for..in
statement to iterate over the indices of an array:
// Create an array of three strings
var empNames = ["Ken", "Fred", "Li"];
// Use the for..in statement to iterate over indices of the array
for(var index in empNames) {
var empName = empNames[index];
printf("empNames[%s]=%s", index, empName);
}
empNames[0]=Ken
empNames[1]=Fred
empNames[2]=Li
Search WWH ::
Custom Search