JavaScript Tutorial - ``while'' loop

LeftRight

There are several ways to loop, and we will continue with the ``while'' loop.

The while loop does not have all the nice features of a for loop, and the programmer needs to account for the loop variable initialization prior to the loop body and ``manually'' increment the loop variables in the loop body.

One reason to choose the while loop is that it need not confine itself to loop variables. The expression can be a function.

The basic form for a do/while loop is:

while ( expression ) {
	block of code ...
}

Example: work/whileloop.html

<HTML>
<BODY>
<CENTER>
<TABLE BORDER=1 CELLPADDING=6>
<SCRIPT LANGUAGE="JavaScript">
var i=0,j=0;	// loop variables
while (j <= 10) {
	i=0;
	j++;
	document.write('<TR>');
	while (i <= 10) {
		i++
		document.write('<TD ALIGN="RIGHT">',i*j,'</TD>');
	}
	document.write('</TR>\n');	// end the line
}
</SCRIPT>
</TABLE>
</CENTER>
</BODY>
</HTML>

HOMEWORK:

  1. Use the functions F2C() and C2F() defined in a previous assignment and create a conversion table. Use the values 0 to 100 in increments of 2. Display the value, the Celsius conversion (F2C), and the Fahrenheit conversion (C2F).
    You can use either <PRE> or <TABLE> formatting.
LeftRight
Slide 10