By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Help regarding Loops?

P: 40
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?
Sep 9 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 9 '06 #2

risby
P: 30
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.
Sep 10 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.