Home arrow JavaScript arrow Javascript: More Loops and Events
JAVASCRIPT

Javascript: More Loops and Events


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.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
November 14, 2007
TABLE OF CONTENTS:
  1. · Javascript: More Loops and Events
  2. · Break Dancing
  3. · And So it Continues
  4. · Finishing Loops

print this article
SEARCH DEVARTICLES

Javascript: More Loops and Events
(Page 1 of 4 )

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials