Assignment 3 - For loops and while loops

You hand in this assignment by showing it to the lecturer when it is completed. For loops and while loops can be translated into each other and this will be done in this exercise in order to train the skills in using them. Nevertheless, there is a philosophical idea behind these two types of loops:

  1. A for loop has the intention to model that one executes a specific task a certain number of times which is known prior to starting the work. The for loop has a counting variable which is used to count the number of times a loop is executed. Here is an example:

    var x;
    var y = 0;
    for(x = 0; x < 1000; x = x + 1)
    {
        y = y + x * x;
    }

    This for loop computes the sum 0²+1²+2²+3²+…+998²+999² of the first 1000 square numbers and stores the result in a variable y. The loop variable is x. It is initialized with 0 and after each execution of the loop body y = y + x * x, the x variable is updated by adding one to it.

  2. A while loop has the intention to model that one runs a loop as long as a certain condition is true where one does not know when this condition becomes false. Such a condition could for example come from user input:

    while(window.confirm("Do you want to play?"))
    {
        tictactoe();
    }

    The tictactoe() function is not shown here, but a later assignment deals with how to implement this game on a computer. In the case that the user has to play the game once first and is then asked whether he wants to continue, one can use a do-while loop (also just called do loop):

    do
    {
        tictactoe();
    } while(window.confirm("Do you like to continue to play?"));
    

For loops are more flexible than it looks like. For example, one can start at any value and go to any value and even do weird updates like in the following: for(x = 64; x < 1000; x = x + x) { … }. In this case the variable has at the first time the value 64, in the second run through the loop the value 128, in the third 256, in the fourth 512. Then, after that loop, the value is 1024 and no longer smaller than 1000, so the loop is just ran four times. This flexibility allows even to simulate while loops by for loops as follows:

while(cond)
{
    commands;
}
becomes
var u;
for(u = cond; u ; u = cond)
{
    commands;
}

where u is a new variable and the condition is always copied into u. As long as u is true, the for loop is executed. Of course, assigning the value of the cond condition to u is a bit non-standard and a while loop would be a much better style in this case as it is much easier to understand here. The translations in this assignment will therefore give more natural for loops, provided they are done properly.

The opposite direction is much easier to do. This for loop:

var x;
var y = 0;
for(x = 0; x < 1000; x = x + 1)
{
    y = y + x * x;
}

can be translated into the following while loop:

var x = 0;
var y = 0;
while(x < 1000)
{
    y = y + x * x;
    x = x + 1;
}

Note that the x = 0 initialization has to be before the while loop, here it is moved into the variable declaration. Furthermore, the x = x + 1; update command has to become the last statement of the body of the loop, that is, it has to be appended to the stuff between the braces { and }. The condition between the two semicolons of the header starting with for at the for loop becomes the condition of the while loop.

  1. Getting Started

    Please refer to this page for information on how to work on your assignment.

  2. Task of this exercise

    In the following text, there are functions fa, wa, fb, wb, fc, wc, fd, wd and so on. The f-version always is a for loop and the w-version a while loop. In each case, one of the functions always returns 0. Please adjust this function such that it does the same as its counterpart. The x input is always a natural number (the {0,1,2,3,…} set).

JavaScript Starts Here.
JavaScript Ends Here.