Help regarding Loops?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitong
    New Member
    • Sep 2006
    • 40

    Help regarding Loops?

    I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    With the use of the break statement any loop can be used to mimic any other loop.

    Generally I would use for loops unless I had a specific reason to use a while or do-while loop.

    The do-while loop is unique of the 3 in that the loop code clock is always executed at least once. You will sometimes get situations where this is useful.

    I would only use the while loop if I didn't really have a control variable that required updating on every iteration.

    Comment

    • risby
      New Member
      • Sep 2006
      • 30

      #3
      Originally posted by bitong
      I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
      I tend to think of using a for loop when I know how many interations I want it to do, as when iterating over an array of a known size, and using a while loop when the number of iterations is unknown in advance, perhaps dependent on the users' choice of whether to proceed.

      Comment

      Working...