Thread: while and for question

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    while and for question

    i was wondering whish is faster for(; or while(true) as for myself i think for(; is faster coz it doesnt chk if value is 1 is just keep going on and on

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    But I don't have a key...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I think most compilers will optimize that code down to the same assembler instructions anyway, so it doesn't matter.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by QuantumPete View Post
    I think most compilers will optimize that code down to the same assembler instructions anyway, so it doesn't matter.

    QuantumPete
    while(true)

    can cause a warning "conditional expression is constant"

    while
    Code:
    for(;;)
    does not such thing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vart View Post
    while(true)

    can cause a warning "conditional expression is constant"

    while
    Code:
    for(;;)
    does not such thing
    Hmm, I wonder why that is. Surely both do the exact same thing?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I remember reading in an old book about optimisation in C that noted that compilers regarded old at the time of writing would produce code that evaluated the constant expression in:
    Code:
    while (true)
    or more accurately, since true is a C++ keyword and C99 macro:
    Code:
    while (1)
    but those same compilers would correctly produce "infinite loop" code when they encountered:
    Code:
    for (;;)
    No doubt this is not true of modern compilers, but it is of historical interest, and thus I decided to prefer the latter because it is arguably more "canonical"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Hmm, I wonder why that is.
    because

    Code:
    while(1)
    could be a mistake and written instead of

    Code:
    while(l)
    for example

    On the same reason
    Code:
    if(f=fopen...)
    could produce a warning about assignment in the if instead of comparison
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    because

    Code:
    while(1)
    could be a mistake and written instead of

    Code:
    while(l)
    So do you think it is a good idea to subject people to warnings for mistakes they did not make?

    Snafuist posted this rather amusing set of related facts the other day.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    So do you think it is a good idea to subject people to warnings for mistakes they did not make?

    Snafuist posted this rather amusing set of related facts the other day.
    I prefer to have a warning about the correct code (I always have a way to silence it here) then not to have a warning about the incorrect code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I always get plenty of

    warning C4706: assignment within conditional expression

    when I do something like

    Code:
    if (!(lf = fopen(LOGFILE_NAME, "a"))) {
        printf("Failure to create or open logfile: %s\n", LOGFILE_NAME);
        ...
        }
    
    When it's exactly what I want to say. But then I always use warning levels = 4 (highest).
    Last edited by nonoob; 02-24-2009 at 05:46 PM.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by nonoob View Post
    I always get plenty of

    warning C4706: assignment within conditional expression

    when I do something like

    Code:
    if (!(lf = fopen(LOGFILE_NAME, "a"))) {
        printf("Failure to create or open logfile: %s\n", LOGFILE_NAME);
        ...
        }
    
    When it's exactly what I want to say. But then I always use warning levels = 4 (highest).
    Change it to this and the warning should go away:
    Code:
    if ( (lf = fopen(LOGFILE_NAME, "a")) == 0 )
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed