Thread: Associativity from right to left in ternary operator

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    Associativity from right to left in ternary operator

    hi,

    i am little bit confusion regarding the Associativity from right to left in ternary operator.

    Associativity comes to picture when Operators on the same line in the chart have the same precedence.
    ex
    z=a+b+c+d+e; then according to associativity it start from left to right.

    same way can u give one example for right to left of tenrary operator if the operands have same precedence?

    i look for your further replys

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Tertiary operation is defined as such

    (condition) ? <if true do this> : <if false do this>


    The basis of performing either the left side of the ':' or the right side operation is all governed by the condition being either true, chose left, or false, chose right.

    That make since?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It means stuff like this:
    Code:
     a? b: c? d: e
    Is the same as this:
    Code:
    (a?b:(c?d:e)
    Instead of this:
    Code:
    ((a?b:c)?d:e)
    It's executed from left to right. A first. Then b or c. Then if C was executed, then d or e.
    Last edited by King Mir; 12-11-2009 at 01:13 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The associativity of the ternary operator is right-to-left. For instance:

    Code:
    a ? b : c ? d : e ? f : g;
    Nests like this:

    Code:
    a ? b : ( c ? d : ( e ? f : g ) )
    I have used this before as an alternative to:

    Code:
    if( a ) b;
    else if( c ) d;
    else if( e ) f;
    else g;
    When doing so, I write it this way:

    Code:
      a ? b
    : c ? d
    : e ? f
    : g;
    Last edited by brewbuck; 12-11-2009 at 01:14 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment operator for static array inside a class
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2008, 11:11 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM
  5. int vs BIG INT
    By rumi_red in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 04:15 AM