Extra Help on loops

Extra 4a

The condition on a do-while loop is always tested at the end of the loop, after the statements in the loop have been carried out. If the condition passes, then the program jumps to the top of the loop and carries out the statements again. If the condition fails, the program drops through the bottom of the loop and carried on with whatever it finds after the loop.

This does mean that the statements in the loop have to be carried out at least once. They may be carried out further times if the condition passes. If, after carrying out the statements once, the condition fails on the first attempt, then the statements aren't carried out again.

Here is an example program:

<script language="JavaScript">
var x = 12;
var a = 1000;
do
 { x = 2 * a;
   a++;
 }
while (a < 100);
document.write("The value of a is " + a + "<br />");
document.write("The value of x is " + x + "<br />");
</script>

Let's examine this in detail. When the program reaches the do part of the loop, a has the value 1000 and x has the value 12. Then the program reaches the instruction x = 2 * a, and so it resets x to twice the value of a, i.e. 2000. After that, the instruction a++ increases the value of a to 1001.

It is at this point, and only this point, that the condition of the loop is first tested. The condition is (a < 100). Clearly, this condition fails, since a is now 1001. This means that the loop terminates immediately, and does not repeat. The two values displayed at the end of the program are therefore 1001 for a and 2000 for x.

Extra 4b

The condition on a while loop is always tested at the start of the loop, before the statements in the loop have been carried out. If the condition passes, then the program continues with the statements in the loop. At the end of the loop, the program jumps back to the start of the loop and tests the condition again. If the condition is still true, then the statements are executed again etc.

If the condition fails the very first time that it is tested, then the statements aren't executed at all. Instead, the program moves to the end of the loop and executes whatever comes after it.

Here's the same program that we saw in the previous example but with a slight change. This time it contains a while loop.

<script language="JavaScript">
var x = 12;
var a = 1000;
while (a < 100)
 { x = 2 * a;
   a++;
 }
document.write("The value of a is " + a + "<br />");
document.write("The value of x is " + x + "<br />");
</script>

What happens this time? Well x and a still start off as 12 and 1000 respectively. This time the condition is tested immediately. Is a < 100? No it isn't! This means that x is not reset to 2000, and a is not increased by 1. Instead, the two statements in the loop are completely missed, and the next statements to be executed are the ones that display the values of a and x. The values displayed are still 12 for x and 1000 for a.

Extra 4c

The three parts of the for loop are all included inside the brackets after the word for. Remember, the first thing inside the brackets is carried out as soon as the loop is encountered. When the last statement that is controlled is encountered, the last thing in the brackets is carried out. Then the condition, the second thing inside the brackets is tested. As long as that condition is true, the loop executes from the first controlled statement again.

for (initialisation; condition; end-of-loop part)
 {
   statement;
   statement;
   statement;
 }

In my experience, 99% of the for loops that you will meet will be simple counting loops where a variable is used to count upwards (or downwards). The way to set up such a counting loop is, firstly, to declare a variable that will be used as a counter (I have called it counter, but, of course, you can call it anything you like) :

var counter;

Then the for loop is declared in the normal manner:

for (counter = 0; counter < 15; counter++)
 { // Statements as per normal
 }

The initialisation part sets the variable to 0. The end-of-loop part adds 1 to the variable just before the condition is tested at the end of the loop. The condition is set to stop when the variable reaches 15, so the variable counter will be 14 the last time that the program passes through the loop. It is important to make sure that you set the condition correctly (see the section below).

It is equally easy to get a counter that counts downwards from one value to another. In this case, you would make one or two changes to the loop as shown:

for (counter = 14; counter >= 0; counter--)
 { // Statements as per normal
 }

In this case the -- symbol indicates that the loop is supposed to count downwards, as it subtracts 1 from the value of the variable. This time the variable is set up in the first part of the loop to the highest value that it should be, i.e. 14, and the condition is set so that the loop will continue to execute as long as the variable holds a number which is 0 or bigger. At the end of the last iteration of the loop, the variable will hold 0. As soon as the program subtracts 1 from the variable, the condition will fail (as counter will hold -1) and the condition will fail (counter no longer holds a value which is 0 or greater).

In fact these two loops both produce the values 0 to 14 for the variable counter, but in a different order. If you want to get a loop to count upwards using some number other than 1 for the increase each time, then adapt the last part of the for loop:

for (counter = 0; counter < 15; counter+=2)
 { // Statements as per normal
 }

This will count upwards in 2s, so counter will take the values 0, 2, 4, 6, 8, 10, 12 and 14 (but not 16, of course!) A loop that counts down is done similarly.

Take a look at this line taken from a program:

for (a = 4; x + b != 200; z++)

You'll notice that there isn't a single variable mentioned twice in that instruction. Although you generally will see the same variable mentioned in all three parts of a for loop (after all, most for loops do simple counting), there is no reason why that should be the case! Of course, you would have to make sure that the statement(s) controlled by the for loop did something sensible with the variables so that the loop would eventually come to an end.