ASP.NET
  Home arrow ASP.NET arrow Page 5 - ASP.NET Basics, Part 4: Looping the Loop
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
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? 
ASP.NET

ASP.NET Basics, Part 4: Looping the Loop
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2003-10-20

    Table of Contents:
  • ASP.NET Basics, Part 4: Looping the Loop
  • Counting Down
  • The Infinite Loop and the Careless Coder
  • Dos and Don'ts
  • For-gone Conclusion
  • The Sound of Breaking Loops
  • End of Play

  • 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


    ASP.NET Basics, Part 4: Looping the Loop - For-gone Conclusion


    (Page 5 of 7 )

    Both the "while" loop and its close cousin, the "do-while" loop, areused when you don't know for certain how many times the program shouldloop. But C# also comes with a mechanism for executing a set ofstatements a specific number of times - and it's called the "for" loop:


    for (initial value of counterconditionupdate counter)
    {
            do 
    this!
    }



    Looks like gibberish? Well, hang on there a minute. The "counter" hereis a C# variable that is initialized to a numeric value, and keeps trackof the number of times the loop is executed. Before each execution ofthe loop, the "condition" is tested - if it evaluates to true, the loopwill execute once more and the counter will be appropriatelyincremented; if it evaluates to false, the loop will be broken and thelines following it will be executed instead.

    Let's look at an example:


    <script language="C#" runat="server">
    void Page_Load()
    {  
           
            
    int number 7;

            
    // use a for loop to calculate tables for that number
            
    for (int x=1x<=15x++)
           {
                    
    output.Text += number " X " " = " + (number*x) +
    "
    "
    ;
            }
           
    }      
    </script>
    <html>
    <head><title>Turning The Tables, ASP.NET-Style!</title></head> <body>
    <h3>Turning The Tables, ASP.NET-Style!</h3> <asp:label id="output"
    runat="server" /> </body> </html> 



    And here's the output:

    Turning The Tables, ASP.NET-Style!

    7 X 1 = 7
    7 X 2 = 14
    7 X 3 = 21
    7 X 4 = 28
    7 X 5 = 35
    7 X 6 = 42
    7 X 7 = 49
    7 X 8 = 56
    7 X 9 = 63
    7 X 10 = 70
    7 X 11 = 77
    7 X 12 = 84
    7 X 13 = 91
    7 X 14 = 98
    7 X 15 = 107


    Let's dissect this a little bit.


    <%
    int number 7;
    %>



    Right up front, a variable is defined, containing the number to be usedfor the multiplication table. I've used 7 here - you might prefer to useanother number.


    <%
    // use a for loop to calculate tables for that number
    for (int x=1x<=15x++)
    {
            
    output.Text += number " X " " = " + (number*x) + "
    "
    ;
    } %> 



    Next, a "for" loop has been constructed, with "x" as the countervariable. If you take a look at the first line of the loop, you'll seethat "x" has been initialized to 1, and is set to run no more than 15times.

    Finally, the script takes the specified number, multiplies it by thecurrent value of the counter, and displays the result on the page byvirtue of the "Text" attribute of the "output" label server control.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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