BrainDump
  Home arrow BrainDump arrow C:Loops
Iron Speed
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 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
eWeek
 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? 
BRAINDUMP

C:Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-17

    Table of Contents:
  • C:Loops
  • While You Were Away
  • Do it While You Can
  • The Break And Continue Statement

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    C:Loops
    (Page 1 of 4 )

    We left off discussing operators and how to use them in our last article. Here, we will cover those loops I've been threatening to cover for so long. You will learn of their many and powerful uses. Behold! The mighty loop...

    Loop Dee Loop

    Loops in C are used to repeat a certain block of code a given amount of times. This is what computing is really all about. It saves the programmer time and coding. There are several loops, or iterators as they are also referred to in C, and we will discuss them below, starting with the For Loop.

    For Loop

    For loops repeat a given number of times, so long as the criteria is true. The amount of times the loop is executed is decided in advance. For loops look like this: For(i=0;<10, i++). They consist of three sections. The i=0 portion initializes a counter variable (in this instance, "i"). The <10 tells the program to continue looping while i<10. And finally the i++ is known as an incrementor, and adds one to the counter variable every time the loop is repeated. Here is an example program that counts up to ten and then prints some text:


    #include <stdio.h>

    int main()

    {

    int count;

    printf("nYou have until the count of ten...");

    for(count=0; count <10; count++)

    printf("%dn",count);

    printf("Time is up!");

    return(0);

    }

    The result of this program is:

    You have until the count of ten...

      0

      1

      2

      3

      4

      5

      6

      7

      8

      9

      Time is up!

    We could of course always create a program that counts down using what is called a decrementor:



    #include <stdio.h>

    int main()

    {

    int count;

    printf("nLift-off in t-minus 10 seconds...n");

    for(count=10; count>0; count--)

    printf("T-minus %dn",count);

    printf("Lift off!");

    return(0);

    }

    This program results in:

      Lift-off in t-minus 10 seconds...

      T-minus 10

      T-minus 9

      T-minus 8

      T-minus 7

      T-minus 6

      T-minus 5

      T-minus 4

      T-minus 3

      T-minus 2

      T-minus 1

      Lift Off!

    You will note that we could have accomplished the same thing using this code:


    #include <stdio.h>

    int main()

    {

    int count;

    printf("nLift-off in t-minus 10 seconds...n");

    for(count=10; count>0; count=count-1)

    printf("T-minus %dn",count);

    printf("Lift off!");

    return(0);

    }

    And we aren't simply limited to incrementing/decrementing by 1 either. Behold this mighty example! 

    #include <stdio.h>

    int main()

    {

    int count;

    for(count=0; count<9; count=count+2)

    printf("%d ",count);

    printf("Who do we appreciate?!?");

    return(0);

    }

    In the above code, we increment by 2 each time through the loop, resulting in:

      0 2 4 6 8 Who do we appreciate?!?

    You can use multiplication as well. In this sample we increment by *52 and count to a million:



    #include <stdio.h>

    int main()

    {

    int count;

    for(count=2; count<1000000; count=count*52)

    printf("%d ",count);

    printf("You counted to a million!");

    return(0);

    }

    Note that the initial value of count is set to 2. Had we set it to zero, we would have created an infinite loop. This is because if we set the initial value to 0, 0 * 52 equals 0, and so the value never would have equaled 1,000,000.

    Okay, one last thing before we finish our discussion of for loops. We don't always have to print the number out. Let us suppose we just want to print some text ten times. Here is how we would do so:


    #include <stdio.h>

    int main()

    {

    int count;

     

    for(count=0; count<10; count++)

    printf("James Payne is awesome and my hero! In him, I do obey!n

    ");

    return(0);

    }

    This results in:

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

      James Payne is awesome and my hero! In him, I do obey!

    More BrainDump Articles
    More By James Payne


       · Hey, welcome to my article on C Loops. where we discuss the different types of loops...
     

    BRAINDUMP ARTICLES

    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager
    - Generating Clients and Services with Indigo
    - Vista SP1, A Review
    - Services and the WCF
    - VBScript: Final Date Functions
    - Creating Services with the WCF
    - The Resource View of the MFC
    - VBScript: More Fun with the Date Functions
    - Vista Price Cuts Dissected

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway