Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | 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 |

break and continue Statements - JavaScript(JS)

The break and continue statements alter the flow of control.

break and continue Statements

 

The break and continue statements alter the flow of control. The break statement, when executed in a while, for, dowhile or switch statement, causes immediate exit from the statement. Execution continues with the first statement after the structure. The break statement is commonly used to escape early from a loop or to skip the remainder of a switch statement (as in Fig. 8.7). Figure 8.11 demonstrates the break statement in a for repetition statement.

 

During each iteration of the for statement in lines 14–20, the script writes the value of count in the XHTML document. When the if statement in line 16 detects that count

 

      <?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.11: BreakTest.html -->

 

      <!-- Using the break statement in a for statement. -->

 

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

 

      <head>

 

      <title>

 

        Using the break Statement in a for Statement

 

        </title>

 

        <script type = "text/javascript">

        <!--

 

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

{

if ( count == 5 )

            break; // break loop only if count == 5

        document.writeln( "Count is: " + count + "<br />" );

 

} //end for

document.writeln(

        "Broke out of loop at count = " + count );

        // -->

 

        </script>

 

        </head><body></body>

 

</html>


Fig. 8.11 | Using the break statement in a for statement.

is 5, the break in line 17 executes. This statement terminates the for statement, and the program proceeds to line 22 (the next statement in sequence immediately after the for statement), where the script writes the value of count when the loop terminated (i.e., 5). The loop executes line 19 only four times.

 

The continue statement, when executed in a while, for or dowhile statement, skips the remaining statements in the body of the statement and proceeds with the next iteration of the loop. In while and dowhile statements, the loop-continuation test eval-uates immediately after the continue statement executes. In for statements, the incre-ment expression executes, then the loop-continuation test evaluates. This is the one case in which for and while differ. Improper placement of continue before the increment in a while may result in an infinite loop.

 

Figure 8.12 uses continue in a for statement to skip the document.writeln statement in line 20 when the if statement in line 17 determines that the value of count is 5. When the continue statement executes, the script skips the remainder of the for state-ment’s body. Program control continues with the increment of the for statement’s control variable, followed by the loop-continuation test to determine whether the loop should continue executing.

    <?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.12: ContinueTest.html -->

 

    <!-- Using the continue statement in a for statement. -->

 

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

 

    <head>

 

    <title>

 

      Using the continue Statement in a for Statement

 

      </title>

      <script type = "text/javascript">

      <!--

 

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

{

if ( count == 5  )

            continue; //     skip remaining loop code only if count == 5

      document.writeln( "Count is: " + count + "<br />" );

 

      } //end for

      document.writeln( "Used continue to skip printing 5" );

      // -->

 

      </script>

      </head><body></body>

 

      </html>


Fig. 8.12 | Using the continue statement in a for statement.


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


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