Learn Java Script, The label Statement, Lesson No. 25
The label statement lets you specify a reference point in your script. Typically, the label statement is associated with loops. A label statement can be referenced by either the break or continue statement. The syntax of the label statement is shown here:
label:
statements;
In the following example, I have created a label called CounterLoop. When the script executes the first time, the label statement is ignored. Within the while loop, I set up a test that checks to see whether the value of counter is equal to 5. If it is, the script executes the continue statement, which tells the script to skip the rest of the statements in the while statement and continue with the next execution of the loop.
<HTML>
<HEAD>
<TITLE>Script 2.15 – Demonstration of the label statement
</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var counter = 10;
document.write(“<B>Watch me count backwards!</B><BR>”);
CounterLoop:
while (counter > 0) {
counter;
if (counter == 5 ) continue CounterLoop;
document.write(“counter = “, counter , “<BR>”);
}
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>
When you load this script, you will see that the continue statement has the effect of skipping the display of the fifth statement.