Page 378 - Webapplication11_C11_Flipbook
P. 378
Difference Between while and do…while Loops
Though the do while loop and the while loop appear to be identical, they differ in how they are implemented.
while loop do…while loop
The condition is tested at the start of the loop in a while The do while loop tests the condition at the end of the
loop, and if the condition is True, statements inside the loop. So, even if the condition fails, do while runs the
loop are executed. It signifies that the while loop will only statements in the code block at least once.
run the code block if the condition is True.
Also called an entry-controlled loop Also called an exit-controlled loop
Minimum no. of iterations—0 Minimum no. of iterations—1
Nested Loop
When a loop is used inside another loop then it is called a nested loop. All the three loops (for, while and do-while)
provided by JavaScript can be used as nested loop. Syntax to use nested loop is:
Outer loop
{
Inner loop
{
//Statements to be executed in the inner loop
}
//Statements to be executed in the outer loop
}
Example:
<HTML>
<HEAD>
<TITLE> Nested Loop </TITLE>
</HEAD>
<BODY>
<H3> Using Nested Loop</H3>
<SCRIPT>
var i, j;
for(i=1; i <= 5; i++)
{
for(j=1; j<=i; j++)
{
document.write("*");
}
document.write("<br />");
}
</SCRIPT>
</BODY>
</HTML>
376 Touchpad Web Applications-XI

