SunQuest
 
       JavaScript
  Home arrow JavaScript arrow 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
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
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Javascript: More Loops and Events


    (Page 1 of 4 )

    In our last tutorial we covered functions and a portion of loops, leaving off at the While loop. In this tutorial we will continue with the Do While Loop and hopefully work our way through to a taste of JavaScript Events.

    Do While

    The Do While is a variation on our pal the While loop. It is used when you want to execute a block of code at least once. After it does the initial run through, it will then continue to loop while your criteria is met. Once the criteria is no longer met, it will exit the loop and move on.


    <html>

    <body>

    <script type="text/javascript">

    i = 0

    do

    {

    document.write("I've had this many beers: " + i)

    document.write("<br />")

    i++

    }

    while (i <= 4)

    </script>

    </body>

    </html>

    This will print out:

      I've had this many beers: 0
      I've had this many beers: 1
      I've had this many beers: 2
      I've had this many beers: 3
      I've had this many beers: 4

    The way the program works is that it assigns a value to the counter variable (0 in this instance), then goes through the loop once, writing out how many beers I have had. No matter what criteria is met or not met, it will always at least print:

      I've had this many beers: 0

    We then have it print a break to separate the data on separate lines. Finally, it increments the value of our variable by one and checks the criteria at the end (as opposed to at the beginning, which is how we force it to run through at least once). If the criteria is met, it loops again. If not, it breaks out of the loop.

    More JavaScript Articles
    More By James Payne


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

    JAVASCRIPT ARTICLES

    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...

    Iron Speed

    Iron Speed





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