BrainDump
  Home arrow BrainDump arrow Page 3 - 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

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    C:Loops - Do it While You Can
    (Page 3 of 4 )

    A Do While loop is similar to a While loop, except that with a Do While loop, the program always loops through at least one time. In the below example, we will create a program that asks a user for a number and then counts down from there:


    #include <stdio.h>

    int main()

    {

    int count;

    printf("Please enter a number from 1-20: ");

    scanf_s("%d",&count);

    do

    {

    printf("%dn",count);

    count--;

    }

    while(count>0);

    return(0);

    }

    If you type in 0 in the prompt, you will see that the program still runs, even though the criteria of count>0 is not met. This is because the first time through, the program prints the text prior to seeing whether the condition was met. It will always loop at least once.

    Try running this program and type in the number 23. As you can see, the program still works, even though we told the user to only enter a number from 1-20. To fix this, we can insert a second loop. Let's do so:


    #include <stdio.h>

    int main()

    {

    int count;

    do

    {

    printf("Please enter a number between 1-20: ");

    scanf_s("%d",&count);

    }

    while(count<1 || count>20);

    do

    {

    printf("%dn",count);

    count--;

    }

    while(count>0);

    return(0);

    }

    Now if you run the program and try to enter anything larger than 20 or smaller than 0 at the prompt, it will loop through and have you make your choice again until you choose an appropriate number.

    Other Uses for Loops

    You can also use loops to create a dramatic pause in your program. Let's count down from ten and have it pause in between each loop:


    #include <stdio.h>

    int main()

    {

    int count;

    int wait;

    count=10;

    do

    {

    printf("%in",count);

    count--;

    for(wait=0;wait<1000000000;wait++);

    }

    while(count>0);

    return(0);

    }

    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




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