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:
- 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 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^2+1^2+2^2+3^2+...+998^2+999^2
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
variable x is updated by adding one to it.
- 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 function "tictactoe()" 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 condition "cond" 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. The above 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 initialization "x=0" has to be before the while-loop,
here it is move into the variable-declaration. Furthermore,
the update-command "x=x+1;" 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" af the for-loop
becomes the condition of the while-loop.
Downloading the assignment.
Log on your UNIX account and go into the correct directory.
cd public_html
Download this page onto your computer and place it on your webpage.
wget http://www.comp.nus.edu.sg/~gem1501/assignment03.html
chmod 755 *.html
The wget command copies a file into your page.
The chmod-command enables you to look with your browser at
the downloaded assignment.
Use an editor like
pico,
vi or vim
to look at the file. You can type the file name as well like
pico assignment03.html
so that the editor knows which file to edit and where to save
the changes.
The task
In the following text, there are functions fa, wa, fb, wb, fc, wc, fd, wd and
so on. The "f"-version has always 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 input x is
always a natural number from the set {0,1,2,3,...}.
Java Script Starts Here.
Java Script Ends Here.