please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.5 for

The for statement is a loop that is often more convenient than the while statement. The for statement takes advantage of a pattern common to most loops (including the while loop example above). Most loops have a counter variable of some kind. This variable is initialized before the loop starts. Then it is tested as part of the expression evaluated before each iteration of the loop. Finally, the counter variable is incremented or otherwise updated at the end of the loop body just before the expression is evaluated again.

The initialization, the test, and the update are the three crucial manipulations of a loop variable, and the for statement combines these three and makes them an explicit part of the loop syntax. This makes it especially easy to understand what a for loop is doing, and prevents mistakes such as forgetting to initialize or increment the loop variable. The syntax of the for statement is:

for(initialize ; test ; increment)
    statement

The simplest way to explain what this for loop does is to show the equivalent while loop:[1]

[1] As we'll see when we consider the continue statement, this while loop is not an exact equivalent to the for loop.

initialize;
while(test) {
    statement
    increment;
}

That is, the initialize expression is evaluated once, before the loop begins. To be useful, this is an expression with side effects, usually an assignment. The test expression is performed before each iteration and controls whether the body of the loop is executed. If the test expression is true, then the statement that is the body of the loop is executed. Finally, the increment expression is evaluated. Again, this must be an expression with side effects in order to be useful. Generally it will be an assignment expression or will use the ++ or -- operators.

The example while loop of the previous section can be rewritten as the following for loop, which counts from 0 to 9:

for(count =index.html 0 ; count < 10 ; count++)
    document.write(count + "<br>");

Notice how this syntax places all the important information about the loop variable on a single line, which makes it very clear how the loop will execute. Also note that placing the increment expression in the for statement itself simplifies the body of the loop to a single statement, and we don't even need to use curly braces to produce a statement block.

Loops can become a lot more complex than these simple examples, of course, and sometimes there will be more than one variable changing with each iteration of the loop. This is the only place that the comma operator is commonly used in JavaScript--it provides a way to combine multiple initialization and increment expressions into a single expression suitable for use in a for loop. For example:

for(i =index.html 0, j = 10 ; i < 10 ; i++, j--)
    sum += i * j;


Previous Home Next
while Book Index for...in

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

[Chapter 5] 5.8 continue +


file: /Techref/language/java/script/definitive/ch05_05.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/26 23:07,
TOP NEW HELP FIND: 
3.21.44.115:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/language/java/script/definitive/ch05_05.htm"> [Chapter 5] 5.5 for</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?