Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javascript Looping In Java Script

Related Tutorials


 
 

Share on Google+Share on Google+

Looping In Java Script

Advertisement
In this article you learn the basics of JavaScript and create your first JavaScript program.

Looping In Java Script

In this article you learn the basics of JavaScript and create your first JavaScript program.

What is JavaScript loop?
 The JavaScript loops used to execute the same block or code a specified number of times and while a specified condition. JavaScript loops Very often write code, you want the same block of code to run over and over again in a row. The Instead of adding several almost equal lines in a script we can use loops to perform a task.

JavaScript supported Two different type looping :-
 1.The For Loop
 2.The while Loop

 For Loop:
The JavaScript for loop is used the know in advance how many times of the script should run.  JavaScript loop Use a For loop to run time same block of code a specified the number.

Example:

<html>
<body>
<script language="javascript">
var i=0
for (i=0;i<=6;i++)
{
alert("The number is " + i)
}
</script>
</body>
</html>


The while Loop:
The while loop is used when the loop to execute and continue executing while the specified condition

<html>
<body>
<script language="Javascript">
var i=0
while (i<=10)
{
document.write("serial number" + i)
document.write("<br />")
i=i+1
}
</script>
</table>
</body>
</html>


The do while loop
The JavaScript do...while loop is a variant of the while loop. The loop will be 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 be always executed once, even if the condition is false, because the loop code are executed before the condition is tested.

 Example:

<html>
<body>
<script language="Javascript">
var i=0
do 
{
alert("number" + i)
i=i+1
}
while (i<3)
</script>
</table>
</body>
</html>

 

The for in:
This is not same as the for loop . The javascript for...in loop is used to provide to the enumerate properties of a JavaScript object . This loop only found in JavaScript . This statement in the loop are executed for each property of an object until every property has been accessed.

Example:

<html>
<head> 
<Script language="Javascript">
var i;
var num= new Array(8,4,5,7,10);
for(i in num)
{
document.write(" " ,num[i]);
document.write("<br />")
}
</script>
</head>
</body>
</html>

Javascript looping Break and continue
There are two statements supported in javascript used in loop:- Break and continue

Break:
The JavaScript break command will break the loop and continue executing the code the follows after the loop.

Example:

<html>
<head>
<Script language="Javascript">
var i=0
for (i=0;i<=6;i++)
{
if (i==3){break}
alert("number" + i)
}
</script>
</head>
</body>
</html>

Continue:
The JavaScript continue command will be break the current loop and continue with the next value. 

Example

 

<html>
<head>
<script language="JavaScript">
var i=0;
for (i=0;i<=8;i++)
{
if (i==4){continue}
document.write("value" + i)
document.write("</br>")
}
</script>
</head>
</body>
</html>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 13, 2008

Related Tutorials

Discuss: Looping In Java Script  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
aunda jackson manyara
September 14, 2011
java script,c++

this subject is so brilliant
Charulata Chaudhari
June 26, 2014
code

how set the time in three separate text box i.e 1st box show the hours ,2nd box show the minutes and third box show the suffix i.e. am and pm.....how to write in loop...........?
DMCA.com