Iteration

main > javascript > js statements > js iteratives

These tutorials are about JavaScript and its use for ViSTA programming.

Some statements in JavaScript are known as iterative statements. These statements have control structures that delimit them and which determine how many times (zero or more) the delimited code is executed, based on some condition.

We will look at three structures here

The while Statement

The while statement is the most basic of iterative control structures. It repeatedly runs a block of code until some condition is no longer true. If the condition is not true to begin with, then the code never runs. It takes the form of:

while (expression) {
  statements;
  }

For example:

var x = 0;
while (x < 10) {
  a = a + b[x];
  x++;
  }

In the above code, we initialize a variable to zero (0) before beginning the loop. The while statement then evaluates the conditional expression. This expression must be inside parentheses. In this case , zero is less than 10, so the statement executes.

In the body of a while statement there must be some code that changes the value of the condition. Otherwise the loop will never terminate.

The last line in the statement block increments the variable we are testing in our conditional expression. In the body of a while statement there must be some code that changes the value of the condition. Otherwise the loop will never terminate. In the following code, the conditional expression will never become false, so the code will run forever (or until it crashes).

//  bad code -- an infinite loop
var x = 0;
var z = 0;
while (x < 10) {
  a = a + b[z];
  z++;
  }

infinite loop: an iterative code element that has no valid termination condition, meaning it will never stop looping

This state of endless execution is known as an infinite loop. Anyone with any programming experience is all too familiar with them. Their causes are many, and they are far too easy to create.

The do ... while Statement

A while statement will never execute if its conditional expression never evaluates to true. What happens if you want the code to always run at least once, regardless of the value of its conditional expression?

The answer is use a do ... while statement.

A do ... while statement is like a while statement in reverse. First it runs the block of code, then it evaluates the conditional expression. If the conditional expression is true, then it loops back to the beginning of the statement and starts again.

It takes the form of:

do {
  statements;
  }
while (expression);

Note that since the curly brackets do not end the entire statement, we need a semi-colon at the end.

An example:

var x = 0;
do {
  a = a + b[x];
  x++;
  }
while (x < 10);

This code will have the same effect as the equivalent while loop above. One that would work differently migh be as follows:

// will never run
// since the initial state of x
// is x != 10
var x = 0;
while (x == 10) {
  a = a + b[x];
  x++;
  }

// will run once
// even though the initial state of x
// is x != 10
var x = 0;
do {
  a = a + b[x];
  x++;
  }
while (x == 10);

A more practical use for this loop might be to check whether something has a value. For instance, you could walk an array as long as you keep finding values.

var x = 0;
do {
  if (aName[x]) {
    bName[x] = someProc(aName[x]);
    }
  x++;
  }
while (aName[x]);

In the preceeding example, we take advantage of the fact that non-existent value return false. This example assumes all the array elements are contiguous, since it will stop executing with the first empty array element it finds.

The for Statement

The for statement allows for incremental processing based on some incremental variable.

Unlike the while statement, the variable is incremented within the conditional expression itself. This means that you do not have to have code in the body of the statement to set the termination condition.

Once again, the statement keeps executing as long as the conditional statement is true.

It takes the form of:

for (init; condition; increment) {
  statements;
  }

The three values that go inside the for statement conditional expression are:

init
The initial state of the variable to be tested. Normally done as an assignment, although the assigned value can be from any source, literal or variable.
condition
The condition to be tested for. The statement keeps processing as long as it remains true.
increment
The increment by which the variable being tested changes.

For example:

for (x = 0; x < 10; x++;) {
  a = a + b[x];
  x++;
  }

For those who want to do things the hard way, you can also specify multiple conditions by putting them in a comma separated list within the semi-colon separated list.

for (x=0,y=0; x<10,y<20; x++,incrF(y);) {
  a = a + b[x];
  x++;
  }

for and while are not one-hundred percent interchangeable, so think about which one you want to use before coding.

The for statement assumes some sort of incrementation, such as counting through a set of something. It is designed for stepping through things.

flag: a variable, usually boolean, for reporting the current state of something in a program

Although any basic discussion of iteration tends to use incremental examples for both while and for statements, the while statement is better suited for testing some flag or condition. A flag, in programming terms, is a variable, usually boolean, that refers to the current state of something the in the program. Our example above of using a while statement to walk an array is an example where we are testing the contents of the elements of an array, not our position in it.

The for ... in Statement

The for ... in statement is a special case of the for statement. It is used exclusively to walk through arrays and objects. In the case of arrays, it allows you to step through each array element. For objects, it steps through the properties of the object. The benefit of this statement is that to you would otherwise have to determine the number of elements in an array or object before you could use a for statement. And unlike a while statement, it tests for the end of the set automatically, so you don't have to.

It takes the form of:

for (varName in objName) {
  statements;
  }

objName is the name of the object or array you want to walk through. varName is the name of a variable to accept the property name or array position of the object or array being walked. You can then refer to that specific position in the object or array through standard array notation.

For example:

for (elName in navigator) {
  document.write(elname);
  document.write(" = ");
  document.write(navigator[elName]);
  document.write("<br />");
  }

enumerable: literally means able to be counted

When walking through an object, the for ... in statement will not step through every property. It will only step through those that are defined as enumerable. In other words, it will only step through those those are defined as properties that can be counted by JavaScript iterative processes. This is to avoid presenting the user will a great deal of garbage, since there are many system-defined properties associated with any object.

System-defined methods are not enumerable, though most system-defined properties are. User defined properties are always enumerable. In Opera some very important system-defined objects are not enumerable at all, meaning you have to know your property names, because you can't use this to just look them up. Just a little inconvenient.