JavaScript Tutorial/Statement/For Loop

Материал из Web эксперт
Версия от 11:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Check the loop counter

   <source lang="javascript">

<html> <head> <title></title> </head> <body> <script type="text/javascript"> for (var i = 1; i < 101; i++) {

   if (i == 99) {
       alert("The number is " + i);
   }

} </script> </body> </html></source>


Counting backward through a for loop

   <source lang="javascript">

<html> <head> <title>Back Racer</title> <script> var i = 0; for (i = 10; i > 0; i--){

 alert(i);

} </script> </head> <body> </body> </html></source>


for Loops

The for loop is a structure that loops for a preset number of times.

The for loop is made up of two parts: condition and statement.

The condition structure determines how many times the loop repeats.

The statement is what is executed every time the loop occurs.

The condition structure is contained within parentheses and is made up of three parts.

Each part is separated by a semicolon (;).

The first part of the condition structure initializes a variable to a starting value.

In most cases, the variable is declared within this section as well as initialized.

The second part is the conditional statement.

The third and final part determines how the variable should be changed each time the loop is iterated.

The format of the for loop looks like the following:



   <source lang="javascript">

for (initialize; condition; adjust) {

     statement;
   }</source>
   
  

For loop with undeclared loop counter

   <source lang="javascript">

<HTML> <HEAD> <SCRIPT language="JavaScript">

</SCRIPT> </HEAD> <BODY>

</BODY> </HTML></source>


Multiplication Table Generator by using for loop

   <source lang="javascript">

<html> <head> <title>Multiplication Table Generator</title> <script language="javascript" type="text/javascript">

</script> </head> <body> <a href="javascript:generateTable()">Create New Table</a> </body> </html></source>


Reversed for loop

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Set loop step to 5 in for loop

   <source lang="javascript">

<html> <head> <title>Count By Five</title> <script> var i = 0; for (i=5; i <= 100; i += 5){

 alert (i);

} </script> </head> <body>

Count By Five

</body> </html></source>


Use for statement to loop through all elements in an array

   <source lang="javascript">

<html> <head> <title>Use for statement to loop through all elements in an array</title> </head> <body>

<script language="javascript" type="text/javascript"> </script>

</body> </html></source>


Using the JavaScript for Loop

   <source lang="javascript">

<HTML> <HEAD> <TITLE> Using the JavaScript for Loop </TITLE> </HEAD> <BODY>

Using the JavaScript for Loop

<SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </BODY> </HTML></source>