ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 - Dos and Don'ts


    (Page 4 of 7 )

    A close cousin of the "while" loop is the "do-while" loop, whichgenerally looks like this:


    do
    {
            do 
    this!
    } while (
    condition)

    Wondering what the difference is between this one and the previous one?Try this next example in your browser:


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

            
    // script will never enter the loop
            // since the condition is false
            
    while (bingo == 699)
            {      
                    
    output.Text "Bingo!";
            }
    }      
    </script>
    <html>
    <head><title>Dos and Don'ts</title></head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    And here is the output:



    What output?

    The reason for the blank page is simple. If you take a closer look atthe code listing above, you will notice that the conditional statementevaluates to false at the first iteration itself. As a result, thescript never enters the "while" loop.

    Now, there may be occasions when you need to execute a particular set ofstatements at least once before you check for a valid conditionalexpression. Consider the example of an interactive session with a storemanager adding items to the store inventory. You might want to force himto add at least one item. Once he has added a single item, you can askif he wishes to add more; if yes, allow him to continue, otherwiseterminate the session. A "do-while" loop fits this situation well: theconstruction of the "do-while" loop is such that the statements withinthe loop are executed first, and the condition to be tested is checkedafter.

    The next example will demonstrate this:


    <script language="C#" runat="server">
    void Page_Load()
    {  
           
            
    int bingo 366;
           
            do
            {      
                    
    output.Text "Bingo!";
            } while (
    bingo == 699);
    }      
    </script>
    <html>
    <head><title>Dos and Don'ts</title></head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    And here is the output:

    Bingo!



    In this case, the script will always execute the statements within theloop at least once. Subsequent iterations are subject to the evaluationof the conditional expression of the "do-while" loop. In the exampleabove, the expression evaluates to false, resulting in immediate exitfrom the loop after the first iteration. This is clearly seen in theoutput above.

    How about another example to make things clearer? This next one rewritesthe countdown example using the "do-while" loop below:


    <script language="C#" runat="server">
    void Page_Load()
    {  
           
            
    // define a loop counter
            
    int countdown10;
           
            
    output.Text "Beginning countdown...";
           
            
    // repeat so long as the variable
           // is greater than or equal to zero
            
    do {
                    
    output.Text += "
    countdown;
                    
    countdown--;   
            } while (
    countdown >= 0);
           
            
    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>



    And here's the output:

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


    Houston, we have lift-off!



    The bottom line: using a "do-while" loop ensures that the code withinthe loop will be executed at least once, regardless of whether or notthe conditional expression evaluates as true.

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