BrainDump
  Home arrow BrainDump arrow C:Loops
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? 
BRAINDUMP

C:Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    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
     
     
    ADVERTISEMENT


    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

    - Firefox on Windows Mobile
    - Microsoft`s Sneaky Firefox Add-On Installati...
    - Microsoft`s Answer to Google Search - Bing
    - Doing Statistical Analysis with MS Excel
    - Extracting Google-Indexed Web Site Pages Usi...
    - Advanced String Manipulation Using MS Excel
    - Using Goal Seek and Solver in MS Excel
    - Internet Explorer 8: A Hands-on View
    - Tools for Beginning Game Developers
    - XAMPP: Tips for Running an Apache/MySQL Serv...
    - Improving Your Visual Studio Workspace
    - A Look at Microsoft`s Mobile Operating System
    - Microsoft Internet Explorer 8: Mixed Reactio...
    - Configuring WSUS 3.0 on Windows Server 2008
    - Migrating to Windows Server 2008





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