JAVASCRIPT LOOPS
JAVASCRIPT LOOPS
JAVASCRIPT LOOPS
Types of Loops
1. for loop
2. while loop
3. do-while
4. for/in
5. for/each
Loops : The main use of loops are if we want to perform same work for number of times i.e repetative work at those places we will use loops
1. FOR LOOP
For loop is easy readable
If you know starting and where to stop you will use For loop
Example :
for (var x=1;x<11>
Document.write ("Ahamad");
}
Example :
<html>
<head>
</head>
<body>
SCRIPT
for (x=1;x<11>
Document.write (x*2+"
");
}
SCRIPT
</body>
</html>
2.WHILE LOOP
It can br infinity loop [or] if you don,t know where to stop condition you will use while loop
WHILE LOOP WILL EXXECUTE ONLY WHEN THE WHILE CONDITION SATISFIED
<html>
<head>
</head>
<body>
SCRIPT
var x=1;
while (x<15>
Document.write (x*2+"
");
x++;
}
SCRIPT
</body>
</html>
3. DO-WHILE
<html>
<head>
</head>
<body>
SCRIPT
var x=11;
do {
Document.write (x*3+"
");
x++
} while (17<15>
/*while (x<15>
Document.write (x*3+"
");
x++;
}
*/
/SCRIPT
</body>
</html>
CONTINUE
BREAK
<html>
<head>
</head>
<body>
SCRIPT
/* for (x=1;x<11>
if (x==5) {
// break;
Document.write (x*2+"*
");
} else {
Document.write (x*2+"
");
}
}*/
for (x=1;x<11>
if (x==5) {
// break;
Document.write (x*2+"*
");
continue;
}
Document.write (x*2+"
");
}
/*var x=11;
do {
Document.write (x*3+"
");
x++
} while (17<15>
/*while (x<15>
Document.write (x*3+"
");
x++;
}
*/
SCRIPT
</body>
</html>