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 / 28
    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...
       · Thankyou for the information its in very simple and understandable...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





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