ASP.NET
  Home arrow ASP.NET arrow Page 2 - 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 - Counting Down


    (Page 2 of 7 )

    The most basic loop available in C# is the "while" loop, and it looks like this:


    while (condition)
    {
            do 
    this!
    }



    Or, to make the concept clearer,


    while (rich Uncle Ed's still breathing)
    {
            be nice to him
    }



    The "condition" here is a standard C# conditional expression, whichevaluates to either true or false. So, were you told to write the aboveexample in C#, it would look like this:


    while (rich_old_guy_lives == 1)
    {
            
    be_nice();
    }



    The "while" control structure tests the conditional expression first,and only proceeds to execute the statements within the loop if theexpression evaluates to true.

    How about an example to demonstrate the "while" loop?


    <script language="C#" runat="server">
    void Page_Load()
    {  
            
    // define a variable for the countdown
            
    int countdown 10;
           
            
    output.Text "Beginning countdown...";
           
            
    // repeat the statement so long as the
            // variable is greater than or equal
            // to zero
            
    while(countdown >= 0)
           {
                    
    output.Text += "
    countdown;
                    
    countdown--;   
            }
           
            
    output.Text += "<h1>Houston, we have lift-off!</h1>";
           
    }      
    </script>
    <html>
    <head><title>Countdown To Launch</title></head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    Here is the output.

    Beginning Countdown...
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0

    Houston, we have lift-off!



    Each loop requires a variable, or "loop counter," to track the number of iterations. In the example above, this variable is called "countdown." I have initialized it to 10, since the loop will start counting down from that value.

    Next, we have the "while" loop proper:


    <%
            while(
    countdown >= 0)
           {
                    
    output.Text += "
    countdown;
                    
    countdown--;   
            }
    %>



    This is pretty straightforward stuff. The "while" statement will checkif the value of the "countdown" variable is greater than or equal tozero. If it is, it will enter the loop and execute the enclosedstatements (in this case, print the value of the variable followed by aline break). It then decrements the value of the "countdown" variable byone (using the -- operator discussed previously), and repeats theprocedure.

    On the eleventh iteration, the value of the countdown variable will be-1. Therefore, the result of the conditional statement will be false,the loop will be exited and the statements following the loop will beprocessed.

    What if you forget to decrement the value of the loop counter (a commonnewbie mistake)? Start praying, and flip the page.

    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 4 hosted by Hostway
    Stay green...Green IT