# Thread: Associativity from right to left in ternary operator

1. ## 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?

2. 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. It means stuff like this:
Code:
` a? b: c? d: e`
Is the same as this:
Code:
`(a?b:(c?d:e)`
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.

4. 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;```