Java Certification, Programming, JavaBean and Object Oriented Reference Books
Lesson Seven - Repetition

Now that you know how to do decision making, let's figure out how to make this process repeat until we are done. This is called looping. Loop statements repeat until the specified condition becomes "true", or the program has found what the user selected.

One way of achieving this is by the use of while statements. For example:

var count = 1;
while (count <= 5) {
 document.write(count);
 ++count;
}
document.write("You have printed 5 numbers.");

The program above starts at 1 and prints the numbers 1 through 5, then informs the user that they have printed 5 numbers. Although this may be a simplistic version of how the while statement is used, any program that uses the while statement runs upon the same principle. You have a variable that you start with, condition it to a certain term, and print or run a program to operate until the condition meets the term. Once the program meets the term, you need a condition, such as the line ++count;, to change the condition of the statement. If you leave this out, you will be stuck in what is called an infinite loop, or a loop that never ends. This means that if you leave out the counting increment, you will never see how many numbers you have printed because you will be printing the numbers from 1 to infinity.

Another looping statement is the do...while statement. With the do...while statement, you run the program first, then test it against the condition. Consider the example:

var count = 1;
do {
 document.write(count);
 ++count;
} while (count <= 5);
document.write("You have printed 5 numbers.");

You still get the same answer, however the program is processed differently.

Along with these loops is the for loop. In this loop, it initializes an expression, evaluates a given condition, then executes the statements if the condition is true. If it's false, or becomes false during execution, the loop ends. If it remains in execution, the update statement increments by one. For example:

for (var count = 1; count <= 5; ++count) {
 document.write(count);
}
document.write("You have printed 5 numbers.");

There is another looping statement that has a more simple function. It's called the for...in loop. This loop cuts the memory allocation space down considerably by using a common property to assign to an object. For example:

function Animal(type, sound, transport_mode) {
 this.animal_type = type; // object property
 this.animal_sound = sound; // object property
 this.animal_transport_mode = transport_mode; // object property
}
livestock = new Animal("cow", "moo", "walk"); // instanitiate object
for (prop in livestock) { // this for loop prints
 document.writeln(livestock[prop]); // the names of the properties
}

The loop takes the names of the properties and assigns them to the properties in the function. At the same time, it assigns an index to each property. This is similar to how to print an array, however, you cannot refer to these properties outside of the loop.

Another way of assigning values to object properties is to use the with statement. with eliminates the need for repetition of certain words or phrases used in referencing the same object. To show what we mean, consider the program below:

function Animal(type, sound, transport_mode) {
with (this) {
 animal_type = type; // object property
 animal_sound = sound; // object property
 animal_transport_mode = transport_mode; // object property
 }
}
livestock = new Animal("cow", "moo", "walk"); // instanitiate object
for (prop in livestock) { // this for loop prints
document.writeln(livestock[prop]); // the names of the properties
}

Now, you only have to type "this" once and the with statement adds "this" to each property. This also saves some memory when running the program. The less memory your program uses, the faster it will run. This is very important on the internet when you want people to see your web site. The quicker it loads, the more likely the chance that people stay at your site or come back to visit again.

Question: So you say that Java and JavaScript are so different. They're starting to look the same to me. What can you do with JavaScript that you'd have a hard time doing with Java?

Answer: I'm glad you asked. You can use JavaScript to control how your web page functions. To see how this is done, we will learn how to work with windows and frames.

Return to : Java Programming Hints and Tips