10.2 while
Loop
For many programming tasks, you’ll need to perform the same operation repeatedly (e.g., adding a group of numbers to find their sum). To perform operations repeatedly, you’ll need to use a loop statement. JavaScript provides three types of loop statements—while
loop, do
loop, and for
loop. We’ll cover the while
loop in this section, and the do
and for
loops later in the chapter.
Syntax and Semantics
The while
loop is the most flexible of the three types of loops. You can use it for any task that needs repetitive operations. FIGURE 10.1 shows the syntax and semantics for the while
loop. The syntax at the left of the figure should look familiar because it’s similar to the if
statement syntax. In the while
loop’s heading, after ...
Get Web Programming with HTML5, CSS, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.