ASP.NET
  Home arrow ASP.NET arrow Page 6 - 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 - The Sound of Breaking Loops


    (Page 6 of 7 )

    When dealing with loops, there are two important keywords you should beaware of: "break" and "continue". You might remember that I dealt withthese briefly in the previous section of this tutorial; let's now take acloser look, since these keywords come in handy when dealing with loopsas well.

    First, the "break" keyword. This is usually used to exit a loop when itencounters an unexpected situation. A good example of such a situationis the dreaded "division by zero" error: when dividing one number byanother one (which keeps decreasing), it is always advisable to checkthe divisor on every iteration and take action (either exit the loop orskip to the next iteration) once it becomes equal to zero.

    Take a look:


    <script language="C#" runat="server">
    void Page_Load()
    {
            
    // some variables to play with 
            
    int dividend 7;
            
    int divisor 10;
           
            
    // a for loop to perform division
            
    for(int count 1count <= 15count++)
           {
           
                    
    // if divisor is zero
                    // get out now
                    
    if(divisor == 0)
                    {
                            break;
                    }
                   
                  
    // print loop counter and perform division
                    
    output.Text += "Loop counter is " count "." " ";
                    
    output.Text += dividend " / " divisor " = " +
    ((double)
    dividend/divisor) + "
    "
    ;
                  
    // decrement divisor
                    
    divisor--;
            }
           
            
    output.Text += "<h3>Divisor is zero, time to stop!</h3>";
    }      
    </script>
    <html>
    <head></head>
    <body>
    <h3>Divide And Conquer</h3>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    Here's the output:

    Divide And Conquer

    Loop counter is 1. 7/10 = 0.7
    Loop counter is 2. 7/9 = 0.777777777777778
    Loop counter is 3. 7/8 = 0.875
    Loop counter is 4. 7/7 = 1
    Loop counter is 5. 7/6 = 1.166666666666667
    Loop counter is 6. 7/5 = 1.4
    Loop counter is 7. 7/4 = 1.75
    Loop counter is 8. 7/3 = 2.333333333333333
    Loop counter is 9. 7/2 = 3.5
    Loop counter is 10. 7/1 = 7

    Divisor is zero, time to stop!



    In this example, I have defined two variables, a dividend and a divisor.Next, I've used a "for" loop to divide the two values by each other afixed number of times (15). Note that I have also decremented the valueof the "divisor" variable by one for every iteration of the loop.

    Obviously, at some point of time, "divisor" will become zero. Anyattempt to continue division after this point will result in ugly C#error messages about illegal operations. This is where the "break"statement comes in - it can be used to exit the "for" loop as soon asthe value of the variable "divisor" becomes zero.

    Should I pat myself on my back? Not really.

    Take another look at the output and compare it against the code listing.You will see that the code exited the "for" loop before the conditionfor the loop became false.

    Ideally, the script should exit the "for" loop when the value of the"count" variable becomes greater than 15, and, until then, shouldperform division for all values of the divisor other than 0 (includingnegative values).

    In the example above, the "break" statement caused it to exit once thedivisor becomes 0 (on the tenth iteration of the loop). Therefore, onlypositive divisor values are taken care of by the code above; divisionoperations involving negative values of the divisor never even see thelight of day.

    Is there a way to fix this? Sure, with the "continue" keyword. Take alook:


    <script language="C#" runat="server">
    void Page_Load()
    {
            
    // some variables to play with 
            
    int dividend 7;
            
    int divisor 10;
           
            
    // a for loop to perform division
            
    for(int count 1count <= 15count++)
           {
           
                    
    // if divisor is zero
                    // decrement divisor and continue
                    
    if(divisor == 0)
                    {
                            
    divisor--;
                            continue;
                    }

                  
    // print loop counter and perform division
                    
    output.Text += "Loop counter is " count "." " ";
                    
    output.Text += dividend " / " divisor " = " +
    ((double)
    dividend/divisor) + "
    "
    ;
                    
    divisor--;
            }
           
            
    output.Text += "<h3>All done, time to stop!</h3>";
    }      
    </script>
    <html>
    <head></head>
    <body>
    <h3>Divide And Conquer</h3>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    Here's the output:

    Divide And Conquer

    Loop counter is 1. 7/10 = 0.7
    Loop counter is 2. 7/9 = 0.777777777777778
    Loop counter is 3. 7/8 = 0.875
    Loop counter is 4. 7/7 = 1
    Loop counter is 5. 7/6 = 1.166666666666667
    Loop counter is 6. 7/5 = 1.4
    Loop counter is 7. 7/4 = 1.75
    Loop counter is 8. 7/3 = 2.333333333333333
    Loop counter is 9. 7/2 = 3.5
    Loop counter is 10. 7/1 = 7
    Loop counter is 12. 7/-1 = -7
    Loop counter is 13. 7/-2 = -3.5
    Loop counter is 14. 7/-3 = -2.3333333333333333
    Loop counter is 15. 7/-1 = -1.75

    All done, time to stop!



    In this revised example, when the value of the "divisor" variablebecomes zero, I decrement its value and call the "continue" keyword.This "continue" keyword skips over the current iteration of the loop andsend control of the program back to the start of the "for" loop for thenext iteration (unlike the "break" statement, which simply exits theloop immediately). As a result, the script continues to iterate throughthe "for" loop until the conditional statement is no longer true, andthe value of the "count" variable becomes greater than 15.

    For obvious reasons, there is no output for that particular iteration ofthe loop when the value of "divisor" variable is zero.

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