BrainDump
  Home arrow BrainDump arrow Page 3 - 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 - 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...
       · 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 2 Hosted by Hostway
    Stay green...Green IT