07. JavaScript Loops

Javascript Loops

The for Loop
The while Loop
The do...while Loop

Loops execute a block of code a specified number of times or while a specified condition true.

In Javascript, there are 2 kinds of loops:
for. Loops through 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

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

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

<html>
<body>
<script type="text/javascript">
for(i=0;i<=5;i++)
{
document.write("The number is "+i);
document.write("<br/>");
}
</script>
<p>Explanation:</p>
<p>This for loop starts with i=0.</p>
<p>As long as <b>i</b>is less than, or eual to 5, the loop will continue to run.</p>
<p><b>i</b> will increse by 1 each time the loop runs</p>
</body>
</html>

<html>
<body>
<script type="text/javascript">
for(i=1;t<=6;i++)
{
document.write("<h"+i+"> This is heading"+i);
document.write("</h"+i+">");
}
</script>
</body>
</html>

The while Loop

The while loop loops through a block of code a specified number of times or while a specified condition is true.

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

The <=could be any comparing statement

The Distance between the for and while is that in the for loop,
the conditions are known and can be specified beforehand.
the while loop is used when the initial conditions are known,
but the terminal condition is discovered as th block is executed.

<html>
<body>
<script type="text/javascript">
i=0;
while(i<=5)
{
document.write("the number is " +i);
document.write("<br/>");
i++;
}
</script>
<p>Explanation:</p>
<p><b>i</b> is equal to 0.</p>
<p>while <b>i</b> is lessthan, or equal to, 5, the loop will continue to run.</p>
<p><b>i</b>will increase by 1 each time the loop runs.</p>
</body>
</html>

The do...while Loop

this loop executes the block of code once, and than it will repeat the loop as long as the specified condition is true.

do
{
code to be executed
}while(condition)

The difference between the while and do..while loops should be characterized by whether the condition is checked before or aftr the block is executed.
In the case of the while loop, the condition is checked first, so if false, the block will not be executed. In the do...while loop, the condition is checked after the block is executed;
therefore the block is always executed at least once.

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

<p>Explanation:</p>
<p><b>i</b> equal to 0.</p>
<p> the loop will run</p>
<p><b>i</b> will increase by 1 each time the loop runs</p>
<p>while<b>i</b> is less than, or equal to, 5, the loop will continue to run.</p>
</body>
</html>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams