`Comparability' of code snippets (from book `Code complete')

Discussion in 'C Programming' started by lovecreatesbea..., Nov 18, 2007.

  1. The book `Code complete' mentions similar code snippets as the
    followings , and talks about their advantages respectively.

    But I think these code snippets are totally different in logic, and
    their functionalities are different. They don't have comparabilities.
    Am I right?


    /*code 1*/
    for (i = 0; i < N; i++){
    if (cond)
    DoSomething();
    else
    DoOtherthing();
    }

    /*code 2*/
    if (cond){
    for (i = 0; i < N; i++)
    DoSomething();
    } else {
    for (i = 0; i < N; i++)
    DoOtherthing();
    }
     
    lovecreatesbea..., Nov 18, 2007
    #1
    1. Advertisements

  2. said:
    It depends on details that are not shown.
     
    Richard Heathfield, Nov 18, 2007
    #2
    1. Advertisements

  3. Assuming no attempt to alter cond, i or N in the functions, they should have
    exactly the same effect.

    The question is which is more efficient, and which is better style.
     
    Malcolm McLean, Nov 18, 2007
    #3
  4. Provided neither DoSomething() nor DoNothing() alter the value of cond
    or i, they're identical, and a good optimising compiler should be able
    to generate the same machinecode.
     
    Mark McIntyre, Nov 18, 2007
    #4
  5. lovecreatesbea...

    santosh Guest

    They have the same effect but only if 'i' and 'cond' are not altered by
    either of the functions. If they are going to be volatile the first
    form is the correct one.
     
    santosh, Nov 18, 2007
    #5
  6. It depends on whether we want to evaluate ``cond'' just once, or once
    for each iteration. We just don't have enough information to determine
    which one is correct.
     
    Keith Thompson, Nov 18, 2007
    #6
  7. if cond is in fact a macro that has side-effects.

    But then maybe "if" has been redefined by the preprocessor, if you're going
    to get like that.
     
    Malcolm McLean, Nov 18, 2007
    #7
  8. lovecreatesbea...

    Tor Rustad Guest

    The answer depends on weather 'cond' is a loop-invariant, or not.

    The compiler can sometimes tell, and might then replace the first code
    snippet with the second, during the optimizing.
     
    Tor Rustad, Nov 18, 2007
    #8
  9. Get like what?

    You snipped part of the article to which I replied, in which santosh wrote:

    They have the same effect but only if 'i' and 'cond' are not
    altered by either of the functions. If they are going to be
    volatile the first form is the correct one.

    If "cond" is declared volatile, then the number of times it's evaluated
    obviously can be relevant. It needn't be a macro.

    For that matter, "cond" could be an abbreviation for the word "condition", and
    could refer (in pseudo-code) to any arbitrary expression used as a condition.
    It's not entirely clear whether the code was meant to be taken literally. But
    even if it was, the issue is volatility.
     
    Keith Thompson, Nov 18, 2007
    #9
  10. lovecreatesbea...

    CBFalconer Guest

    You can't redefine 'if' and have a legitimate program.
     
    CBFalconer, Nov 19, 2007
    #10
  11. lovecreatesbea...

    James Kuyper Guest

    What does "legitimate program" mean, in this context? I don't believe
    that redefining a keyword has undefined behavior, is a syntax error, or
    violates any constraints. It's a very bad idea, but that's a different
    question entirely.
     
    James Kuyper, Nov 19, 2007
    #11
  12. When cond is invariant while the loop is iterating (always true or
    always false), they appear to be equivalent. Can you describe a
    situation where they do not perform the same?

    When cond changes over the course of the loop, they are very obviously
    different. code 1 executes DoSomething or DoOtherthing depending on
    the *current* state of cond while code2 executes the same function
    (either DoSomething or DoOtherthing) for every iteration depending
    only on the *initial* state of cond.




    Remove del for email
     
    Barry Schwarz, Nov 19, 2007
    #12
  13. lovecreatesbea...

    CBFalconer Guest

    I had to search a while. From N869:

    6.4.1 Keywords

    Syntax
    [#1]
    keyword: one of
    auto enum restrict unsigned
    break extern return void
    case float short volatile
    char for signed while
    const goto sizeof _Bool
    continue if static _Complex
    default inline struct _Imaginary
    do int switch
    double long typedef
    else register union

    Semantics

    [#2] The above tokens (case sensitive) are reserved (in
    translation phases 7 and 8) for use as keywords, and shall
    not be used otherwise.
     
    CBFalconer, Nov 20, 2007
    #13
  14. lovecreatesbea...

    James Kuyper Guest

    My apologies - I should have performed the search, not you. But I was
    pretty certain there was nothing to find, which tends to remove the
    motivation to look.
     
    James Kuyper, Nov 20, 2007
    #14
  15. lovecreatesbea...

    Flash Gordon Guest

    James Kuyper wrote, On 20/11/07 05:14:
    Isn't the #define and replacement handled in one of the earlier
    translation phases making that section irrelevant as far as whether you
    can do a #define of if?
     
    Flash Gordon, Nov 20, 2007
    #15
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.