About the For/In Statement

The for/in statement is used to execute a loop iteration for each property in an object. You might remember that an object is like a list except each element is given a name instead of an index number. For/in allows you to extract those property names dynamically.

This statement has the following format:

for( variable in object )
  statement

The following code displays the name and value of each property in the object named box:

var box={length:3,width:4,height:2};
for(var name in box)
  print(name+" = "+box[name]+"\n");
flush;

Executing this code causes DScript to display the following text:

height = 2
length = 3
width = 4

Note that the object properties are not extracted in the same order as they were defined. DScript stores properties inside an object in alphabetical order. So, a for/in statement always extracts properties in alphabetical order as well.