Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Iterative Structures - Part II

Author : Exforsys Inc.     Published on: 27th Jul 2007

JavaScript Iterative Structures - Part II

In this JavaScript tutorial, you will learn about JavaScript Iterative Structures, while loop and do..while loop explained along with syntax and examples.

Ads

while loop:

This looping structure is used when a programmer wants the block of code to execute until the condition specified remains true.

The general format of while loop in JavaScript is as follows:


while(variable relational_operator value)
{
   ………….
   ……………     
//Block of Code to be executed till the 
                specified condition is true

}

An example used to understand the while loop of JavaScript:


<html>
   <
body>
      <
script type="text/javascript">
         var exfor=1
         while (exfor<=15)
         {
         document.write("Value is: " + exfor)
         document.write("<br />")
         exfor=exfor+1
         }
      </
script>
   </
body>
</
html>

The output of the above example is

Value is: 1
Value is: 2
Value is: 3
Value is: 4
Value is: 5
Value is: 6
Value is: 7
Value is: 8
Value is: 9
Value is: 10
Value is: 11
Value is: 12
Value is: 13
Value is: 14
Value is: 15

In the above example, the variable exfor initializes with a starting value of 1. The value of exfor is compared with the condition specified in while loop. Since 1 is less than 15, it returns a true value and the block of code inside the while loop executes, prints the value of exfor (1) and increments the value of variable exfor (resulting in value 2) for exfor. Again, the control transfers to the beginning of while loop and, again, the current value of exfor (2) is compared with 15 and since it returns true again, the block of code inside the while loop executes. This proceeds until the value of exfor is 16. When the value of variable exfor reaches 16, it is compared with the condition and, since 16 is greater than 15, the while condition returns false and control passes out of while loop.

do..while loop:

The do….while loop has similar functionality to the while loop. The only difference being that the condition is placed at the end of the block of statements in a do..while loop. As a result, the block of code executes at least once. If, after the first run, the condition is true, the block of code executes again and continues until the condition becomes false.

The general format of do…while loop in JavaScript is as follows:


do
{
............
............ 
//Block of Code to be executed
}
while (variable relational_operator value)

for example:


<html>
   <
body>
      <
script type="text/javascript">
         var exfor=1
         do
         {
         document.write("Value is: " + exfor)
         document.write("<br />")
         exfor=exfor+1
         }
         while (exfor<1)

      </script>
   </
body>
</
html>

The output of the above program is

Value is: 1

Ads

In the above program, the value of variable exfor is first initialized to 1, then the do block executes, resulting in the printing of the value of variable exfor. Then the value of exfor increments, giving the variable exfor a value of 2. The condition in while loop executes, giving a value of 2 (not less than one), returning a false value. The control is then moved out of the do..while loop structure.



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments