Java script looping

By | May 3, 2018

Java script loops-:  Loops in java script are used to execute the same block of code a specified number of times or while a specified condition is true.

When we are using java script language there are many time we get a  situation when we need to come out of a loop without reaching its bottom. There may also be a situation when you want to skip a part of your code block and start the next iteration of the loop.

very often when we write code we want the same block of code to run over and over again in a row. instead of adding several almost equal lines in a script we can use loops to perform a task like this.

in java script has a two type loops 

For – loops throw a block of code a specified number of times.

while-Loops through a block of code while a specified condition is true.

The for loop – this loop is used when we know in advance how many times the script should run. as like 

<html>
<head>
<title>tips and technic of computer and internet</title>

</head>
<body>
<script language=”javascript” type=”text/javascript”>
var i=0
for(i=0; i<=10; i++)
{
document.write(“The number is”+i)
document.write(“<br/>”)
}

</script>
</body>
</html>

The while loop-: This loop is used when we want the loop to execute and continue executing while the specified condition is true . as like example

<html>
<head>
<title>tips and technic of computer and internet</title>

</head>
<body>
<script language=”javascript” type=”text/javascript”>
var i=0
while( i<=10;)
{
document.write(“The number is”+i)
document.write(“<br/>”)
i=i=1
}

</script>
</body>
</html>

The do….while loop -: it is a variant of the while loop. This loop will always execute a block of code ONCE and then it will repeat the loop as long as the specified condition is true.This loop will always be execute at least once,even if the condition is false, because the code is execute before the condition is tested. as like

<html>
<head>
<title>tips and technic of computer and internet</title>

</head>
<body>
<script language=”javascript” type=”text/javascript”>
var i=0
d0
{
document.write(“The number is”+i)
document.write(“<br/>”)
i=i=1
}
while( i<0;)

</script>
</body>
</html>

 

Leave a Reply

Your email address will not be published. Required fields are marked *