Google

Lesson Seven


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 or until the condition equals "false". 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 without incrementing "count". To get out of this loop, you may need to press Ctrl + Alt + Delete to escape. You can also count down in a loop by changing "count" to any variable greater than 1, the condition being "count > 0", and ++count; to --count;. But don't let that limit you. You can also use *= to increment through the fast way. Say you wanted to multiply by 2. You could have the program below:

var count = 1;
while (count <= 100) {
document.write(count);
count *= 2;
}

This program will multiply "count" by 2 until it reaches 128 (64 * 2) and print each calculation except 128 because it's greater than 100.

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.


Putting it all together!

Here's how decision making works. Below is a cartoon quiz to see how well you know your cartoons. Once you're done, hit the Score button and see how well you did.

Cartoon Quiz

Answer all of the questions on the quiz. Each question will alert you if you answered correctly.

1. What is the name of Walt Disney's famous mouse?

Mighty Mouse
Mickey Mouse
Marty Mouse
Melvin Mouse

2. The character Buzz Lightyear was featured in which animated film?

Fantasia
Hercules
Toy Story
Mulan

3. Pluto is a dog. What is Goofey?

A bear
A mule
A horse
Also a dog

4. Who was always trying to eat Tweety Bird?

Porky Pig
Yosemite Sam
Sylvester
Foghorn Leghorn

5. What is Winnie the Pooh's favorite food?

Honey
Molasses
Peanut Butter
Yogurt


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.


Lesson Six | Home | Lesson Eight

If you have a questions about any of the lessons, feel free to ask.

1