ASP.NET
  Home arrow ASP.NET arrow Page 5 - Finishing an Introductory Look at CIL
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

Finishing an Introductory Look at CIL
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-11-26

    Table of Contents:
  • Finishing an Introductory Look at CIL
  • Local Variables
  • Loops
  • Loops Continued
  • The Outer Loop
  • The Inner Loop
  • Conclusion

  • 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


    Finishing an Introductory Look at CIL - The Outer Loop


    (Page 5 of 7 )

    Returning to our main task, we now need to recreate the loops of our program in CIL. Here's the body of our method:


     for (int i = 2; i <= 1000000; i++)

    {

     bool prime = true;

     int limit = (int)Math.Sqrt(i);

     for (int f = 2; f <= limit; f++)

    {

     if (i % f == 0)

    {

     prime = false;

     break;

    }

    }

     if (prime)

    {

     Console.WriteLine(i);

    }

    }

     Console.WriteLine("Done!");


    As you can see here, we have two loops, one nested in the other. Let's first tackle the outer loop, which is really quite simple. This is done in exactly the same way as our last example, with one exception. Remember how we eliminated the boolean variable? This is because rather than being forced to continue the rest of the outer loop after breaking from the inner one, we can instead jump directly to the next iteration of the outer loop. This is done by jumping straight to the incrementation stage. So, this stage will not require a label (if this isn't clear now, it will become clear later):


    ldc.i4.2

    stloc i


    outer_condition:


    ldloc i

    ldc.i4 1000000

    bgt outer_done


    // CONTENTS OF THE LOOP GO HERE

    ...


    outer_counter:


    ldc.i4.1

    ldloc i

    add

    stloc i


    br.s outer_condition


    outer_done:


    ldstr "Done!"

    call void [mscorlib]System.Console::WriteLine(string)


    If you understood the last example, then this part shouldn't be strange at all. We're doing the exact same thing here, with the exception of an added label.

    More ASP.NET Articles
    More By Peyton McCullough


       · Hello,This is a continuation of the last article. It finishes the main example...
     

    ASP.NET ARTICLES

    - 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
    - Building a Simple Storefront with LINQ





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