Thread: difference among loops

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Question difference among loops

    wat is difference among loops?
    can anybody answer this question?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean? What sort of loops are we talking about? The four basic loop constructs in C, or something else?

    If the former:
    for loop: used when you want to get from some value to another value iteratively.
    while loop: do something zero or more times.
    do-while loop: do something one or more times.
    goto: don't use unless you are REALLY sure you have to. [Which is very rare].



    --
    Mats
    Last edited by matsp; 05-05-2009 at 09:37 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    To show possible practical uses:

    for loop: used when working with every element in a series
    while loop: used when working until a certain condition is met (until the users exits, or until a certain procedure is successful)
    do-while: pretty much the same as above, it just happens AT LEAST once, no matter what.
    goto: used when hacking the kernel, and that's about it, pretty much.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by rakeshkumar View Post
    wat is difference among loops?
    can anybody answer this question?
    1.for loop
    Code:
    for(i=1;i<=5;i++)
    printf("FOR");
    In for loop first i is initialized to 1 then it is checked against the condition(<=5) and then executes the body, after which it increments i and again checks for the condition and the process is repeated until i equals 6.
    2.while loop
    Code:
    i=1;
    while(i<=5)
    {
    //do something
    i++;
    }
    You can see the difference between the structures of for and while loop. They both generally do the same thing.
    3.do while loop
    This loop is used if anything is to be done atleast once.
    Code:
    do
    {
    //do something
    }while(i<=5);
    This loop guarantees that the body will be executed at least once while the other two loops (for and while) may not execute the body even once.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  2. Replies: 5
    Last Post: 09-05-2007, 07:37 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM