Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Labeled break and continue Statements - JavaScript(JS)

Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Control Statements II

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Labeled break and continue Statements - JavaScript(JS)

The break statement can break out of an immediately enclosing while, for, do…while or switch statement.

Labeled break and continue Statements

 

The break statement can break out of an immediately enclosing while, for, dowhile or switch statement. To break out of a nested set of structures, you can use the labeled break  statement. This statement, when executed in a while, for, dowhile or switch statement, causes immediate exit from that statement and any number of enclosing repetition statements; program execution resumes with the first statement after the enclosing labeled statement (a statement preceded by a label). The labeled statement can be a block (a set of statements enclosed in curly braces, {}). Labeled break statements commonly are used to terminate nested looping structures containing while, for, dowhile or switch state-ments. Figure 8.13 demonstrates the labeled break statement in a nested for statement.

 

      <?xml version = "1.0" encoding = "utf-8"?>

 

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

      <!-- Fig. 8.13: BreakLabelTest.html -->

 

      <!-- Labeled break statement in a nested for statement. -->

 

      <html xmlns = "http://www.w3.org/1999/xhtml">

 

      <head>

 

      <title>Using the break Statement with a Label</title>

 

        <script type = "text/javascript">

        <!--

 

stop: { // labeled block

for ( var row = 1; row <= 10; ++row )

{                      

for ( var column = 1; column <= 5 ; ++column )

{                      

if ( row == 5 )

            break stop; // jump to end of stop block

                       

document.write( "* " );

} //end for

                       

document.writeln( "<br />" );

} //end for

                       

// the following line is skipped

        document.writeln( "This line should not print" );

 

        } // end block labeled stop

        document.writeln( "End of script" );

        // -->

 

        </script>

 

        </head><body></body>

 

</html>


Fig. 8.13 | Labeled break statement in a nested for statement.

The labeled block (lines 12–28) begins with a label (an identifier followed by a colon). Here, we use the label stop:. The block is enclosed between the braces at the end of line 12 and in line 28, and includes both the nested for statement starting in line 13 and the document.writeln statement in line 27. When the if statement in line 17 detects that row is equal to 5, the statement in line 18 executes. This statement terminates both the for statement in line 15 and its enclosing for statement in line 13, and the program proceeds to the statement in line 30 (the first statement in sequence after the labeled block). The inner for statement executes its body only four times. Note that the document.writeln statement in line 27 never executes, because it is included in the labeled block and the outer for statement never completes.

 

The continue statement proceeds with the next iteration (repetition) of the immedi-ately enclosing while, for or dowhile statement. The labeled continue statement, when executed in a repetition statement (while, for or dowhile), skips the remaining statements in the structure’s body and any number of enclosing repetition statements, then proceeds with the next iteration of the enclosing labeled repetition statement (a rep-etition statement preceded by a label). In labeled while and dowhile statements, the loop-continuation test evaluates immediately after the continue statement executes. In a labeled for statement, the increment expression executes, then the loop-continuation test evaluates. Figure 8.14 uses the labeled continue statement in a nested for statement to cause execution to continue with the next iteration of the outer for statement.

 

    <?xml version = "1.0" encoding = "utf-8"?>

 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

    <!-- Fig. 8.14: ContinueLabelTest.html -->

 

    <!-- Labeled continue statement in a nested for statement. -->

 

    <html xmlns = "http://www.w3.org/1999/xhtml">

 

    <head>

 

    <title>Using the continue Statement with a Label</title>

 

      <script type = "text/javascript">

      <!--

 

nextRow: // target label of continue statement

for ( var row = 1; row <= 5; ++row )

{       

document.writeln( "<br />" );

         

for ( var column = 1; column <= 10; ++column )

{       

if ( column > row )

          continue nextRow; // next iteration of labeled loop

         

document.write( "* " );

} //end for

      } //end for

      // -->

 

      </script>

 

      </head><body></body>

 

      </html>

 


Fig. 8.14 | Labeled continue statement in a nested for statement.

 

The labeled for statement (lines 13–24) starts with the nextRow label in line 12. When the if statement in line 19 in the inner for statement detects that column is greater than row, line 20 executes and program control continues with the increment of the con-trol variable of the outer for statement. Even though the inner for statement counts from 1 to 10, the number of * characters output on a row never exceeds the value of row.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.