   when should I use do-while, while, for or even goto?
  I''ve been coding in C++ for a while; unfortunately,  till this very moment I am still clueless about the differences between all the types of loops.All I''ve been told is that don''t use goto. (but, to be honest, I have no idea why shouldn''t I use this one)I am also aware of the fact that ''while'' loop tests its condition before execution of the contents of the loop begins; the ''do'' loop tests it condition after it''s been executed at least once.I heard that sometimes you should use a certain type of loops to optimize the program, but, again, I have no idea when and why.I would really appreciate if somebody could explain the differences to me.Regards,Richard 
  Yup you pretty much got it.Use do..while if you want the body to always execute at least once.Use while if there are cases where you might not want the body of the loop to be reached at all.It's a pretty tiny difference (and it's definitely possible to shoe-horn a do-while to work as a while, or the other way around). After you've written a lot of loops, you'll notice how useful it is to have both types.And yeah, never use goto.oh yeah: as far as optimization, there isn't any significant difference in performance between the two. There might be some really tiny difference, but at this point in your coding education, you should just assume that they are the same.   [edited by - pinacolada on February 12, 2004 8:24:33 PM];quote:Original post by vietpub  All I''ve been told is that don''t use goto. (but, to be honest, I have no idea why shouldn''t I use this one)void Function ( void ) {  int pie = 3;  if (pie) {    :LABEL    MyClass fnord; //constructor gets called 3 times without a destructor being called! bad!    if (pie) { --pie; goto LABEL; }    goto OTHERLABEL;  }  :OTHERLABEL //destructor for myclass also never called! bad!}  also, goto tends to lead towards sloppy (read: crazy, confusing, and hard to understand sphagetti) code that would be better served by organization (read: seperating into more functions).   -Mike ;  HELL:goto HELL; ;  Another handy control structure to know is the loop-with-exit. Not nearly as common but much cleaner when you actually need it. It simplifies:// Some processing (1) (usually setup/init type code)// ..while ( !isConditionMet() ){ // Some more processing (2) // .. // Repeat of (1)}  Into:while (true) // or some FOREVER macro{  // Processing (1)  if (isConditionMet())    break; // exit loop  // Processing (2)} ;quote:Original post by OrangyTang  Another handy control structure to know is the loop-with-exit. Not nearly as common but much cleaner when you actually need it. It simplifies:// Some processing (1) (usually setup/init type code)// ..while ( !isConditionMet() ){ // Some more processing (2) // .. // Repeat of (1)}   Into:while (true) // or some FOREVER macro{  // Processing (1)  if (isConditionMet())    break; // exit loop  // Processing (2)}   Personally, I find both to be equivalent in terms of code-cleanliness. You either have the extra Proc1 text or the extra if(conditionMet()) text. pretty much the same. ;  Goto?  All I can say is for my project I have personally written over 60k lines of code, and not one time did I even feel the need to use goto ;  An interesting construction I''ve seen before is:do{// Do stuffif(SomethingFailed())break;// Do more stuffif(SomethingElseFailed())break;// Do even more stuff} while(0);  That is, you "break" out of the "loop," which will only execute once, if something goes wrong.  The other (sloppier) way to do this is:// Do stuffif(SomethingFailed())goto BailOut;// etc.bailOut:  However, I strongly urge you to avoid goto if possible.  It just creates messy code that can be a nightmare to debug. ;quote:Original post by capn_midnight  Personally, I find both to be equivalent in terms of code-cleanliness. You either have the extra Proc1 text or the extra if(conditionMet()) text. pretty much the same.   The ''if'' in the middle isn''t extra, its just shifted position. Its still the exact same conditional. However the Proc1 *is* duplicated in the first example, and duplicate code is always a pain when maintaining code.However I agree that which is more readable is largely dependant on the exact situation. Its always handy to have an alternative though. ;quote:Original post by duke  Goto?  All I can say is for my project I have personally written over 60k lines of code, and not one time did I even feel the need to use goto  I''m paraphrasing from some c++/oop guide, I forget which, but it basically said as a rule don''t use goto - the only time you should ever ever use it is if using goto dramatically improves code readability, if the proper OO alternative creates really obscure code. However I have never personally seen a case of this, so I don''t ever use goto. And I am sure that this is(or should be) true of 99.9999% of all c++ coders out there. 