JavaScript
  Home arrow JavaScript arrow Page 2 - Javascript: More Loops and Events
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Javascript: More Loops and Events
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-11-14

    Table of Contents:
  • Javascript: More Loops and Events
  • Break Dancing
  • And So it Continues
  • Finishing Loops

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Javascript: More Loops and Events - Break Dancing


    (Page 2 of 4 )

    If you were to see my enormous body today, you would never know that at one point in my life I was a break dancing master. My slick moves allowed me to defy the laws of gravity. Granted I was like eight years old and fat, but no one could do the worm like me. Word to your mother's uncle.

    In fact, it was the Summer of Foot Loose, (well for me anyway; in retrospect the movie was playing on regular television by the time I saw it) and I made my mother tell me on an hourly basis that I did in fact 1) look like Kevin Bacon and 2) dance like Kevin Bacon.

    Well, since we are talking about breaks (how's that for a segue), you will note that Javascript has to two types of special statements for you to use inside of loops. They are the Break and the Continue.

    Breaking It Down

    The Break command is used to exit a loop and continue on to the code after the loop. Let's say that I didn't really want you to know how many beers I drank. I could write a code that would exit out of the loop when the value of my counter got so high it was embarrassing:

    <html>

    <body>

    <script type="text/javascript">

    var i=0

    for (i=0;i<=10;i++)

    {

    if (i==3){break}

    document.write("I drank this many beers: " + i)

    document.write("<br />")

    }

    </script>

    </body>

    </html>

    This would print out:

      I drank this many beers: 0
      I drank this many beers: 1
      I drank this many beers: 2

    The code works by assigning a starting value to the counter variable, then saying that if the value is less than or equal to 10, keep doing the loop, and increment the value of the counter by 1 each time through the loop.

    It then gives us a conditional statement, in this case an If statement, that says if the value of our counter is equal to 3, break out of the loop and don't print any more, and skip to the next block of code.

    So even though we initially told the loop to run ten times, because our special criteria was met, it broke out of the loop after only three times.

    More JavaScript Articles
    More By James Payne


       · Welcome to my vainglorious article, where I discuss more of the Javascript Loops and...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT