Page 105 - PortGPT_V2.1_C8_Flipbook
P. 105
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
}
Program 4: To display a right-angle triangle using stars with the help of nested loop.
<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>
Loops in JavaScript 103

