Results 1 to 6 of 6

Thread: How to use Break and Continue Statements in JavaScripts?

  1. #1
    Join Date
    Aug 2006
    Posts
    114

    How to use Break and Continue Statements in JavaScripts?

    I have done the C and C++ programming languages. Now I have turned to the scripting languages few months before. I have started with the JavaScript. In that I am unable to use the break and continue statements.!! Can anybody explain me, how to use Break and Continue Statements in JavaScripts.?? Also Please provide me some sample of coding so that I can understand the concept more clearly.
    Thanks in Advance..!!
    |===================|
    |YAY if that made sense...|
    |===================|

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to use Break and Continue Statements in JavaScripts?

    If you want the control over the code of the loop, you can use the break and continue statements. Because the break and continue statements provide stricter control over the execution of code in a loop. As the words suggests, the break statement will break the loop and continue executing the code that follows after the loop. The break statement exits the loop immediately and doing this prevents the further repetition of the code. On the other hand, continue statement exits the current repetition.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: How to use Break and Continue Statements in JavaScripts?

    The following code demonstrates more about the break statements. I think that it will help you to understand. Here is an example for the break statement :
    HTML Code:
    <html>
    <body>
    <script type="text/javascript">
    var i=0;
    for (i=0;i<=15;i++)
      {
      if (i==5)
        {
        break;
        }
      document.write("The number is " + i);
      document.write("<br />");
      }
    </script>
    </body>
    </html>
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Jul 2006
    Posts
    289

    Re: How to use Break and Continue Statements in JavaScripts?

    The following examples will let you know about the difference between the Break and the Continue Statements. First have look at the Break Statement :
    Code:
     var iNum = 0;
    for (var i=1; i < 10; i++) {
       if (i % 5 == 0) {
          break;
       }
       iNum++;
    }
    alert(iNum); //outputs “4”
    Now check the Continue Statement :
    Code:
    var iNum = 0;
    for (var i=1; i < 10; i++) {
       if (i % 5 == 0) {
          continue;
       }
       iNum++;
    }
    alert(iNum); //outputs “8”
    In Continue Statement, the alert displays “8” , the number of times the loop has been executed. when i reaches a value of 5, it returns to the top and the continue statement is executed, causing the loop to skip the expression iNum++.
    Signatures reduce available bandwidth

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to use Break and Continue Statements in JavaScripts?

    Before looking at the combination of both the statements, I would like to recommend you to just have a look on single statements. Since, you have got the coding of the Break statement already, I am providing you with the Continue statement. The continue statement will break the current loop and continue with the next value. Here is the coding for that :
    HTML Code:
    <html>
    <body>
    <script type="text/javascript">
    var i=0
    for (i=0;i<=15;i++)
      {
      if (i==5)
        {
        continue;
        }
      document.write("The number is " + i);
      document.write("<br />");
      }
    </script>
    </body>
    </html>

  6. #6
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use Break and Continue Statements in JavaScripts?

    In simple language, you can say that we use the break statement to terminate a loop, switch, or label statement. The syntax of the break statement is given below, it can either first or second :
    • break;
    • break label;
    If you use the first option, which is break without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following statement. And if you use the second option, break with a label, it terminate the specified labeled statement.

Similar Threads

  1. How to merge multiple Javascripts into one Javascript
    By Fakhry in forum Software Development
    Replies: 9
    Last Post: 12-04-2012, 02:00 AM
  2. Replies: 2
    Last Post: 17-02-2012, 02:09 PM
  3. How to use Break and Continue in PHP?
    By NIcaBoy in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:41 AM
  4. Difference between DML statements and DDL statements
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 11-01-2010, 01:07 PM
  5. RSS Implementaion in JavaScripts
    By Jagdish Gada in forum Software Development
    Replies: 4
    Last Post: 16-03-2009, 02:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,217,750.26394 seconds with 17 queries