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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek