Home » Online Computer Education » Learn Java Script, The label Statement, Lesson No. 25

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.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *