while and for question

This is a discussion on while and for question within the C Programming forums, part of the General Programming Boards category; i was wondering whish is faster for(; or while(true) as for myself i think for(; is faster coz it doesnt ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,310
    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"
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21