Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
JavaScript
Introduction
Variables & Arrays
Functions
If Statements
Case Statements
Loop Statements
Operators
Form Validation
Alert
Date Objects
Window Objects
Javascript Books
Books @ CA Amz
Books @ UK Amz
Books @ US Amz

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing

JavaScript Loops

Loops are set of instructions that repeat elements in specific number of times.  Counter variable is used to increment or decrement with each repetition of the loop.  The two major groups of loops are, For..Next and Do..Loop.  While..Wend is another type of Do..Loop.  The For statements are best used when you want to perform a loop in specific number of times. The Do and While statements are best used to perform a loop an undetermined number of times.
For..Loop syntax example.
for(i=0;i<value; i++)
{
   javascript statement
}

This example increments a variable i from 0 to value and continues performing java script statements while i is less then value.
Here is For..loop example.
<html>
<body>
<script type="text/javascript">
for (i = 0; i <= 4; i++)
{
alert("Current value of i is " + i)
}
</script>
</body>
</html>

This display sequence numbers 1,2,3,4 on alert box. The for loop sets i equal to 0 and continues displaying values on an alert boxes as long as i is less than , or equal to, 4. The value of i will increase by 1 each time the loop runs.
To view how the result would execute, click the button:
While..Loop
The While..Loop structure repeats a block of statements until a specified condition is met. 
Bellow is syntax for While..loop:
while(variable=value)
{
   javascript statement
}
The above syntax simply repeats some javascript statements while condition tested false.
Bellow is simple example of while loop:
<html>
<body>
<script type="text/javascript">
var x=5*5;
x=prompt("What is 5 x 5?");
while (x != 25)
{
alert("Wrong answer");
x=prompt("What is 5 x 5?",0);
}
document.write("Right, the answer is " + x)
</script>
</body>
</html>
For this example, we declared variable x and initialized 25. We set it equal to prompt box. The prompt box asks the product of 5x5 and continues asking until true answer is provided. It also informs each wrong attempts or each loop.
To view how this code will execute, click the button:
Bellow is the same example but performs the test at the end of the loop. This time, Do..Loop is used. Using this loop, true value is tested.
<html>
<body>
<script type="text/javascript">
var x = 5*5;
do
{
x=prompt("What is 5 x 5?",0);
}
while (x != 25)
document.write("Right, the answer is " + x)
</script>
</body>
</html>

The syntax are same for Do..While and While but keep in mind one tests false condition and the other tests true conation. The execution of this example will be same as the previous example.
Case Statement Operators
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware