SITE SEARCH

SITE LINKS

    • Js Basic
  • JS HOME
  • JS Intro
  • JS HOW TO
  • JS Where To
  • JS Statements
  • JS Comments
  • JS Variables
  • JS Operators
  • JS Comparisons
  • JS If...Else
  • JS Switch
  • JS Popup Boxes
  • JS Functions
  • JS For Loop
  • JS While Loop
  • JS Break Loop
  • JS For..In
  • JS Events
  • JS Try...Catch
  • JS Throw
  • JS Special Text
  • JS Guidelines

  • JS Objects
  • JS Object Intro
  • JS String
  • JS Date
  • JS Array
  • JS Boolean
  • JS Math
  • JS RegExp
  • JS Html DOM

  • JS advanced
  • Js Browser
  • JS Cookies
  • JS Validation
  • JS Animation
  • JS Image Maps
  • JS Timing
  • JS Create Objects
  • JS Summary

  • js examples
  • JS Examples
  • JS Object Examples
  • JS DOM Examples
  • JS QUiz
  • JS Exam

  • JS Reference
  • JS Objects
  • JS HTML DOM

Home Html Css Javascript Php Asp .Net Sql Xml
user login New user? Register Here | Forgot Password?

JavaScript While Loop

Prev
Next

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

The while Loop

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

Syntax

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

Note: The <= could be any comparing statement.

Example

The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

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

The do...while Loop

The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE, and then it will repeat the loop as long as the specified condition is true.

Syntax

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

Example

The example below uses a do...while loop. The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested:

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

Prev
Next
Top

 

 

© 2010 Copyrighted. Yavum.com™