Thread: Do while / while

  1. #16
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    For loops can be equivalent to while loops. But you really should prefer while loops unless your iterations are counted.
    I certainly prefer
    while(1)
    to
    for(;;)

    Also while loops tend to lead to more readable code but thats probably just personal preference.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  2. #17
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Loop zealots annoy me. Use the best tool for the job you're doing. Each loop is made for specific types of behavior, and you should use whatever fits your current purpose, not whatever fits your religious beliefs.

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    It is just about personal preference.
    Bohn Jacopini proved that EVERY loop can be made just with while() and eventually break statements so do<% %>while() and for(;;) are unneeded.
    But as Fol said, every of them have a purpose.
    while() initial check.
    do<% %>while() final check.
    for(;;) + break central check.
    for(e;e;e) initial statement, final check, final statement.

    About good style Stroustrup says (if I recall correcly) that he prefere, sorted by most favourite:
    for(;;) because make life easier when you need final and initial check.
    while() if you do not need initial and final statement it puts the condition right there.
    do<% %>while() if you need final check

Popular pages Recent additions subscribe to a feed