While programming, there might be situation that you need to execute a block of code for a given number of times, in these situation while loop can be used.
The while loop executes code while a condition is true.
Syntax:
while (expression){
Statement(s) to be executed if expression is true
}
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Siple Javascript conditions</title>
</head>
<body>
<button onclick="myLoop()">Click Me</button>
<script>
function myLoop()
{
var count=0;
while(count <=10)
{
document.write("Number: "+count+"<br/>");
count ++;
}
}
</script>
</body>
</html>
The while loop executes code while a condition is true.
Syntax:
while (expression){
Statement(s) to be executed if expression is true
}
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Siple Javascript conditions</title>
</head>
<body>
<button onclick="myLoop()">Click Me</button>
<script>
function myLoop()
{
var count=0;
while(count <=10)
{
document.write("Number: "+count+"<br/>");
count ++;
}
}
</script>
</body>
</html>
Comments
Post a Comment