what is problem with this conditional op ?

Discussion in 'C++' started by abir, Dec 24, 2008.

  1. abir

    abir Guest

    I never fill comfortable with ?: and , operator.
    There is problem with this statement
    int x = 6, y = 20;
    int p = 6;
    p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

    i get here x = 9, y = 62 (unexpected)
    if i write
    p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    62) );
    i get x = 9, y = 61 (expected)

    i even doubt if the second one is correct, though it gives correct
    answer.
    what is the problem here?

    compiler MSVC 9 Express
    thanks
    abir
    abir, Dec 24, 2008
    #1
    1. Advertising

  2. abir

    alfps Guest

    On 24 Des, 14:32, abir <> wrote:
    > I never fill comfortable with  ?: and , operator.
    > There is problem with this statement
    >  int x = 6, y = 20;
    >     int p = 6;
    >     p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;
    >
    > i get here x = 9, y = 62 (unexpected)
    > if i write
    >  p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > 62) );
    > i get x = 9, y  = 61 (expected)
    >
    > i even doubt if the second one is correct, though it gives correct
    > answer.
    > what is the problem here?
    >
    > compiler MSVC 9 Express
    > thanks
    > abir


    This looks like a homework assignment, but hey, it's Christmas.

    The parentheses you added should give you a (strong) clue.

    In the C++ syntax the third expression of a conditional expression
    can't be a comma expression.

    It's akin to but not quite the same as precedence.

    In C and C++ the expression syntax is designed to yield an illusion of
    precedence, which mostly works well as a practical rule of thumb, but
    this illusion breaks down for the combination of conditional and comma
    operators, where you have to understand the syntax instead of
    precedence.


    Cheers & hth.,

    - Alf
    alfps, Dec 24, 2008
    #2
    1. Advertising

  3. abir

    abir Guest

    On Dec 24, 6:59 pm, alfps <> wrote:
    > On 24 Des, 14:32, abir <> wrote:
    >
    >
    >
    > > I never fill comfortable with ?: and , operator.
    > > There is problem with this statement
    > > int x = 6, y = 20;
    > > int p = 6;
    > > p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

    >
    > > i get here x = 9, y = 62 (unexpected)
    > > if i write
    > > p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > > 62) );
    > > i get x = 9, y = 61 (expected)

    >
    > > i even doubt if the second one is correct, though it gives correct
    > > answer.
    > > what is the problem here?

    >
    > > compiler MSVC 9 Express
    > > thanks
    > > abir

    >
    > This looks like a homework assignment, but hey, it's Christmas.
    >

    Nop ...
    this is what i got from a software's complex pointer assignment
    scheme
    ,where it was assigning all the wrong pointers.
    Just tried to make it independently compilable program.
    > The parentheses you added should give you a (strong) clue.
    >
    > In the C++ syntax the third expression of a conditional expression
    > can't be a comma expression.
    >

    but the compiler hadn't complained!
    > It's akin to but not quite the same as precedence.

    how it finds a precedence which gives that result?
    can you show the the way the compiler 'thinks' of correct precedence?
    >
    > In C and C++ the expression syntax is designed to yield an illusion of
    > precedence, which mostly works well as a practical rule of thumb, but
    > this illusion breaks down for the combination of conditional and comma
    > operators, where you have to understand the syntax instead of
    > precedence.

    i suspect that , is ignoring some of the assignments. but even in that
    case
    i am not sure how it correctly 'picks' the second condition, but
    incorrectly
    takes the last assignment.
    i really fear those two operators. esp after Eric Niebler's article
    on ?:
    > Cheers & hth.,
    >
    > - Alf


    and happy Christmas :)
    abir
    abir, Dec 24, 2008
    #3
  4. abir

    alfps Guest

    On 24 Des, 15:18, abir <> wrote:
    > On Dec 24, 6:59 pm, alfps <> wrote:
    >
    > > On 24 Des, 14:32, abir <> wrote:

    >
    > > > I never fill comfortable with  ?: and , operator.
    > > > There is problem with this statement
    > > >  int x = 6, y = 20;
    > > >     int p = 6;
    > > >     p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

    >
    > > > i get here x = 9, y = 62 (unexpected)
    > > > if i write
    > > >  p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > > > 62) );
    > > > i get x = 9, y  = 61 (expected)

    >
    > > > i even doubt if the second one is correct, though it gives correct
    > > > answer.
    > > > what is the problem here?

    >
    > > > compiler MSVC 9 Express
    > > > thanks
    > > > abir

    >
    > > This looks like a homework assignment, but hey, it's Christmas.

    >
    > Nop ...
    >  this is what i got from a software's complex pointer assignment
    > scheme
    >  ,where it was assigning all the wrong pointers.
    >  Just tried to make it independently compilable program.> The parentheses you added should give you a (strong) clue.
    >
    > > In the C++ syntax the third expression of a conditional expression
    > > can't be a comma expression.

    >
    > but the compiler hadn't complained!


    Yeah. That means it parses OK. And that means you have a conditional
    embedded in a comma expression, not a comma expression embedded in a
    conditional.


    > It's akin to but not quite the same as precedence.
    >
    > how it finds a precedence which gives that result?


    It doesn't.

    It just follows the syntax rules.

    The syntax is defined as BNF rules.


    > can you show the the way the compiler 'thinks' of correct precedence?


    Check out the syntax for 'expression'.


    > > In C and C++ the expression syntax is designed to yield an illusion of
    > > precedence, which mostly works well as a practical rule of thumb, but
    > > this illusion breaks down for the combination of conditional and comma
    > > operators, where you have to understand the syntax instead of
    > > precedence.

    >
    > i suspect that , is ignoring some of the assignments. but even in that
    > case
    > i am not sure how it correctly 'picks' the second condition, but
    > incorrectly
    > takes the last assignment.
    > i really fear those two operators. esp after Eric Niebler's article
    > on ?:


    Huh, unfamiliar to me.

    :? is both very simple and very complex.

    It's simple in cases like above, complex when the expressions aren't
    of the same type.


    > and happy Christmas :)
    > abir


    u2. Cheers. I'm off to Christmas dinner... :)

    - Alf
    alfps, Dec 24, 2008
    #4
  5. abir <> writes:

    > I never fill comfortable with ?: and , operator.
    > There is problem with this statement
    > int x = 6, y = 20;
    > int p = 6;
    > p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;
    >
    > i get here x = 9, y = 62 (unexpected)
    > if i write
    > p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > 62) );
    > i get x = 9, y = 61 (expected)
    >
    > i even doubt if the second one is correct, though it gives correct
    > answer.
    > what is the problem here?


    ENEP - Error, Not Enough Parentheses.

    Unless you have the surhuman capability to memorize and regurgitate
    flawlessly the grammar and operator precedence rules of C or C++, you
    better fully parenthesize your code. Yes, C++ needs more parentheses
    than Lisp (if you count braces, brackets and angulars), if you don't
    like it, switch to lisp ;-)

    By the way, another advantage of lisp with respect to these
    syntactical elements, is that it doesn't distinguish expressions from
    statements. So you can easily combine any expression:

    C/C++ statement C/C++ expression lisp expression
    ---------------------- ------------------ ------------------------
    if(...) ... else ... ... ? ... : ... (if ... ... ...)
    .... ; ... ... , ... (progn ... ...)
    { vardecl... expr... } broken here (let (vardecl...) expr...)

    etc, with a lot of other broken here boxes in the C/C++ columns.

    (setf a (if (= 0 a)
    (progn (print 'null) 42)
    (let ((b (* 2 a))) (print b) (1- b))))

    cannot be written in C/C++:

    a=(0==a)
    ?(printf("null"),42)
    :{ int b=2*a; printf("%d",b); return b-1;}; // no no...



    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Dec 24, 2008
    #5
  6. abir

    Guest

    On Dec 24, 6:18 am, abir <> wrote:
    > On Dec 24, 6:59 pm, alfps <> wrote:
    >
    > > On 24 Des, 14:32, abir <> wrote:


    > > >     p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;


    > > In the C++ syntax the third expression of a conditional expression
    > > can't be a comma expression.


    > can you show the the way the compiler 'thinks' of correct precedence?


    If the third expression cannot be a comma expression, then this is the
    *almost* equivalent your statement:

    if (p == 5) {
    x = 8, y = 60;
    } else {
    if (p == 6) {
    x = 9, y = 61;
    } else {
    x = 10;
    }
    }
    // the last comma here...
    y = 62;

    I say "almost" because in your version, the value of the whole
    statement is 62. In mine, there are two separate statements.

    Ali
    , Dec 24, 2008
    #6
  7. abir

    Guest

    On Dec 24, 8:32 am, abir <> wrote:
    > I never fill comfortable with  ?: and , operator.
    > There is problem with this statement
    >  int x = 6, y = 20;
    >     int p = 6;
    >     p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;
    >
    > i get here x = 9, y = 62 (unexpected)



    Because it is being parsed as:

    ((p == 5) ? (x = 8, y = 60) : (((p == 6) ? (x = 9, y = 61)) : (x =
    10))), (y = 62);

    The "y = 62" is the second expression in a comma expression.


    > if i write
    >  p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > 62) );
    > i get x = 9, y  = 61 (expected)
    >
    > i even doubt if the second one is correct, though it gives correct
    > answer.
    > what is the problem here?



    The root problem is the use of ?: when it may have been clearer to use
    other constructs, e.g.:

    if (p == 5) {
    x = 8;
    y = 60;
    } else if (p == 6) {
    x = 9;
    y = 61;
    } else {
    x = 10;
    y = 62;
    }

    That clearly states your intentions and does not suffer the same
    tricky syntax issues.

    Jason


    > compiler MSVC 9 Express
    > thanks
    > abir
    , Dec 25, 2008
    #7
  8. abir

    James Kanze Guest

    On Dec 24, 2:32 pm, abir <> wrote:
    > I never fill comfortable with ?: and , operator.


    There's no problem with ?:. You're problem is that you're using
    the comma operator.

    > There is problem with this statement
    > int x = 6, y = 20;
    > int p = 6;
    > p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;


    Not surprising. The problem is precedence. Nominally, the
    comma operator has lower precedence than ?:. Except that every
    thing between the ? and the : must have higher precedence if the
    code is to be compilable. (Otherwise, you'ld have a
    sub-expression with a ? and no :.) Some good general rules:

    -- Don't use the comma operator. Ever. It only causes
    -- confusion.

    -- Except for the simplest expressions, add parenthese around
    the second and third operand of ?: (but most of the time,
    the expressions should be simple enough it doesn't matter).

    -- Format ?: on several lines, as if it were an if (or a
    sequence of if/else if), e.g.

    p == 5
    ? (x = 8, y = 60)
    : p == 6
    ? (x = 9, y = 61)
    : (x = 10)
    , (y = 62) ;

    (This corresponds to what you wrote. From your comments, I
    gather that it isn't what you wanted.)

    > i get here x = 9, y = 62 (unexpected)


    It's what you wrote.

    > if I write
    > p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
    > 62) );
    > i get x = 9, y = 61 (expected)


    > i even doubt if the second one is correct, though it gives
    > correct answer.


    It's correct, although I'd skip the outer parathese around the p
    == 6 ? expression. (It's basically an else if.) And format on
    several lines.

    > what is the problem here?


    Precedence.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 25, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Romano
    Replies:
    0
    Views:
    487
    J. Romano
    Feb 21, 2004
  2. J. Romano
    Replies:
    0
    Views:
    458
    J. Romano
    Feb 21, 2004
  3. Alec S.
    Replies:
    10
    Views:
    10,142
    Alec S.
    Apr 16, 2005
  4. Richard Tierney

    Problem evaluating conditional operator

    Richard Tierney, Jul 27, 2004, in forum: C++
    Replies:
    3
    Views:
    355
    Richard Tierney
    Jul 27, 2004
  5. Mahesh Tomar

    Conditional operator problem

    Mahesh Tomar, Jul 21, 2003, in forum: C Programming
    Replies:
    6
    Views:
    364
    Eric Sosman
    Jul 22, 2003
Loading...

Share This Page