please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.4 while

Just as the if statement is the basic control statement that allows JavaScript to "make decisions," the while statement is the basic statement that allows JavaScript to perform repetitive actions. It has the following syntax:

while (expression) 
    statement

The while statement works like this: first, the expression is evaluated. If it is false, JavaScript moves on to the next statement in the program. If it is true, then statement is executed, and expression is evaluated again. Again, if the value of expression is false, then JavaScript moves on to the next statement in the program; otherwise it executes the statement that forms the "body" of the loop. This cycle continues until the expression evaluates to false, at which point the while statement ends and JavaScript moves on. Note that you can create an infinite loop with the syntax while(true).

You usually do not want JavaScript to perform exactly the same operation over and over again, so in almost all loops, there are one or more variables that change with each iteration of the loop. Since the variables change, the actions performed by executing statement may differ each time through the loop. Furthermore, if the changing variable or variables are involved in the expression, then the value of the expression may be different each time through the loop. This is important, or an expression that starts off true would never change, and the loop would never end! Here is an example while loop:

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

As you can see, the variable count starts off at 0 in this example, and is incremented each time the body of the loop runs. Once the loop has executed ten times, the expression becomes false (i.e., the variable count is no longer less than 10), the while statement finishes, and JavaScript can move on to the next statement in the program. Most loops will have a counter variable like count. The variable names i, j, and k are commonly used as a loop counters, though you should use more descriptive names if it makes your code easier to understand.


Previous Home Next
if Book Index for

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_04.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/12/27 14:35,
TOP NEW HELP FIND: 
3.147.75.217: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_04.htm"> [Chapter 5] 5.4 while</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?