AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 577


Loops


JavaScript supports familiar loop forms including:

for ( [initStatement(s)] ; [logicalExpression(s)] ; [iterationStatement(s)] )
statement or block

while ( expression )
statement or block

do
statement or block
while ( expression );

All three loops are demonstrated here:

for (var i=0; i < 10; i++)
{
document.write(i+"<br />");
}

var i = 0;
while (i < 10)
{
document.write(i+"<br />");
i++;
}

var i = 0;
do
{
document.write(i+"<br />");
i++;
} while (i < 10);

The break and continue statements are commonly found in loop bodies and are
discussed in the next section.

Labeled Statements, Break, and Continue
Statements can be labeled using:

label: statement(s)

Jump to labeled statements in a block using either:

break label;
continue label;

Otherwise:


  • break exits the loop, beginning execution following the loop body.

  • continue skips directly to the next iteration (β€œtop”) of the loop.

Free download pdf