Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> JavaScript >> Loops in Java Script
Loops in Java Script
Loops in Java Script


by Awadhendra Tiwari on 3/15/2011 9:09:38 PM

Views: 3636       Comments: 0

Loops in Java Script

We can use concept of loops when we want to repeat certain task again and again. We can use concept of loops to reduce line of code and make program more readable and presentable.

In java script there is two types of loop. One is fixed loop in which number of repeat ion is known. Such type of loop are implemented by for keyword another one is   known as variable loop in which number of repeat ion is unknown. Or loop iterates certain step of task until an appropriate action evaluates to true. Such type of loop is implemented by using while keyword. In this demonstration firstly we learn how to implement for loop then in further section we learn how to implement while loop.

for  loop in java script

Syntax of for loop in java script

for (<initialization expression>;<Boolean expression>;<change expression>)
{
            Statements to be executed.
}

Examples which represent for loop

In this example I will show you how to print a table of number by using for loop in java script.

<script type="text/javascript">

        function printTable() {

            var num1 = 5, num2 = 10;

            document.write("Print number between 5 to 10. <br /><br />");

            for (i = num1; i <= num2; i++) {

                for (j = 1; j <= 10; j++) {

                    document.write((j * i) + "&nbsp;&nbsp;");

                }

                document.write("<br /><br />");

            }

        }

</script>

Loops in Java Script

while loop in java script

Syntax for while loop

while(<boolean expression>)
{
                stetements to be executed.
}

Example which represent use of while statement

<script type="text/javascript">

        function printMessage() {

            document.write("while loop demo. <br />");

            var count=0;

            document.write("<font face='Lucida' size='5' color='blue'><ol type='i'>");

            while(count < 10)

            {

                document.write("<li> Number of repeation in while loop is  :"+(count+1));

                count++;

            }

            document.write("</ol></font>");

        }

</script>

Output of the following code snippet is as follows

Loops in Java Script

Using break statements in loop

We can use break statement in loops to break loop at certain situation without completing their life cycle. Break is a keyword which is used to create a break statement.

The following example demonstrate use of break statements in loop

<script type="text/javascript">

        function printMessage() {

            document.write("while loop demo. <br />");

            var count=0;

            document.write("<font face='Lucida' size='4' color='Black'><ol type='i'>");

            while(count < 10)

            {

                document.write("<li> Number of repeation in while loop is  :"+(count+1));

                count++;

                if (count == 5) {

                    document.write("<br /> Loop is going to be break. <br />");

                    break;

                }

            }

            document.write("</ol></font>");

        }

</script>

Output of the following code snippet is as follows

Loops in Java Script

for…. in loop in java script

We can use for….in loop in java script when we want to retrieve some values from any collection like array or property where the number of repletion is not known. Then we use concept of for…in loop. In this demonstration I will tell you that how to implement for…in loop in java script.

Following example demonstrate use of for…in loop in java script

function forInLoop() {

            document.write("This example demonstrate use of for...in loop in java script<br />");

            var nameArr = new Array("Ashish", "Amit", "Awadhendra", "Piyush", "Mark", "John", "David");

            document.write("Displaying content of nameArr <br />");

            for (name in nameArr) {

                document.write("Name you have entered  :&nbsp;" + nameArr[name] + "<br />");

            }

}

In this example I had used new Array () method to create an Array object. And for retrieving values from that array I had written nameArr[name].

Output of the following code snippet is as follows

Loops in Java Script

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5022
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2014MindStick. All Rights Reserved.