different loops.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikant
    New Member
    • Jan 2007
    • 1

    different loops.

    Hi friends,
    I am a Photogrammetry technician and few days back started learning C
    language.It is interesting to learn and I am enjoying while preparing very small programs.I want to learn it more.Certain confusions I do have and hope some one can explain me in a simplified manner.I am happy that with the help of "thescript" I can learn a lot.

    I came accross 3 kinds of loops.
    1)for,2)while ,3)do-while.
    I would like to know the difference between them.
    Hope a simplified answer I will have.

    Thank you.

    Srikant Panda.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I'm moving this thread to the C/C++ forum, where I and many other C people will be glad to help you.

    Comment

    • vpawizard
      New Member
      • Nov 2006
      • 66

      #3
      Hello,
      Welcome to C.

      1) for - Used when the number of times the loop will iterate is known in advance. e.g. for(i=0;i<10;i+ +) will iterate 10 times.
      2) do - Used when the number of times the loop should iterate is not known. The loop iterates till the condition becomes false. e.g. while(mychar != '\n')scanf("%s" ,&mychar); will iterate till newline is entered.
      3) do....while - This loop iterates atleast once. Then, it starts behaving like while loop.

      Regards,

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by srikant
        Hi friends,
        I am a Photogrammetry technician and few days back started learning C
        language.It is interesting to learn and I am enjoying while preparing very small programs.I want to learn it more.Certain confusions I do have and hope some one can explain me in a simplified manner.I am happy that with the help of "thescript" I can learn a lot.

        I came accross 3 kinds of loops.
        1)for,2)while ,3)do-while.
        I would like to know the difference between them.
        Hope a simplified answer I will have.

        Thank you.

        Srikant Panda.
        Generally,
        for loop
        Use it when you know before hand how many times the loop will execute.
        while loop
        Use it to loop as long as a certain condition is satisfied

        do-while
        Repeat something until a certain condition is met. (differs from while in that it always executes the loop body at least once)

        You will need to use them to be able to appreciate them more

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by vpawizard
          Hello,
          Welcome to C.

          1) for - Used when the number of times the loop will iterate is known in advance. e.g. for(i=0;i<10;i+ +) will iterate 10 times.
          2) do - Used when the number of times the loop should iterate is not known. The loop iterates till the condition becomes false. e.g. while(mychar != '\n')scanf("%s" ,&mychar); will iterate till newline is entered.
          3) do....while - This loop iterates atleast once. Then, it starts behaving like while loop.

          Regards,
          Agh, vp beat me to it there. Guess my network is a bit slow today.

          Comment

          • vpawizard
            New Member
            • Nov 2006
            • 66

            #6
            Originally posted by r035198x
            Agh, vp beat me to it there. Guess my network is a bit slow today.
            Well it doesn't really matter who posts first. We all work for C/C++, right pal !!!

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by vpawizard
              Well it doesn't really matter who posts first. We all work for C/C++, right pal !!!
              Interesting also that our posts cover exactly the same things about the loops. The question was asked again last week. Maybe should be included in the tutorials.

              Comment

              Working...