Tuesday, May 3, 2011

The structure of FOR

As I read along the chapter 4, some things came into my consideration... considering the FOR statement.

Lets look at this piece d'code:

var mailArchive = {0: "Dear nephew, ... (mail number 1)",
                   1: "(mail number 2)",
                   2: "(mail number 3)"};

for (var current = 0; current in mailArchive; current++)
  print("Processing e-mail #", current, ": ", mailArchive[current]);
 

In this FOR the control variable current is located IN mailArchive, since the object has the values 0, 1, 2, and so on to be looked for, however when working with arrays the book gives us this code:

 var mailArchive = ["mail one", "mail two", "mail three"];

for (var current = 0; current < mailArchive.length; current++)
  print("Processing e-mail #", current, ": ", mailArchive[current]);
 

The control here is now handled by current being minor than the size of mailArchive, because now it does not have the values that where used with the object.
So it is important to note the different ways you can control a lop with different kinds of data.

No comments:

Post a Comment