Archive Pages Design$type=blogging

How to Loop using JavaScript?

Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true. In JavaSc...

Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.

In JavaScript there are two different kinds of loops which are for loop, which loops through a block of code a specified number of times and while loop, which loops through a block of code while a specified condition is true.

JavaScript For Loop


The for loop is used when you know in advance how many times the script should run.

Syntax:

for(var=startvalue; var<=endvalue;var=var+increment)
{
code to be executed
}

Example:

<html>
<head>
</head>
<body>
<script 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 above example defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. i will increase by 1 each time the loop runs.

Preview:


JavaScript While Loop


The while loop is ued when you want the loop to execute and continue executing while the specified condition is true.

Syntax:

while (var<=endvalue)
{
code to be executed
}

Example:

<html>
<head></head>
<body>
<script 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 example above defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. i will increase by 1 each time the loop runs.

Preview:


JavaScript do .... while Loop:


The do .... while loop 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. The lop alway be executed at least once, even if the condition is false, because the code is executed before the condition is tested.

Syntax:

do
{
code to be executed
}
while (var<=endvalue)

Example:

<html>
<head></head>
<body>
<script type="text/Javascript">
var i=0
do
{
document.write("The number is "+i)
document.write("<br/>")
i=i+i
}
while (i<0)
</script>
</body>
</html>

Preview:


JavaScrpt Break and Continue Statements


There are two special statements that can be used inside loops: break and continue.

Break

The break command will break the loop and continue executing the code that follows after the loop.

Example:

<html>
<head></head>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<=10;i++)
{
if (i==3) {break}
document.write("The number is " +i)
document.write("<br/>")
}
</script>
</body>
</html>

Preview:


Continue

The continue command will break the current loop and continue with the next value.

Example:

<html>
<head></head>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<=10;i++)
{
if (i==3) {continue}
document.write("The number is " +i)
document.write("<br/>")
}
</script>
</body>
</html>

Preview:


Read Next:How to Loop Through JavaScript Array?

Related Search Terms

تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max5: How to Loop using JavaScript?
How to Loop using JavaScript?
Max5
https://maxs27.blogspot.com/2015/02/how-to-loop-using-javascript.html
https://maxs27.blogspot.com/
http://maxs27.blogspot.com/
http://maxs27.blogspot.com/2015/02/how-to-loop-using-javascript.html
true
1760660989564394152
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت