I do know that conditional operators have right associativity, but can't understand how the flow of condition1 , condition2 , expression1 , expression2 , expression3 are all happening one after another. Or how the evaluation and flows are changing when we assume that associativity is left to right.
(condition1) ? (expression1) : ((condition2) ? (expression2) : (expression3)) ;
I asked Gemini and Copilot for an explanation, and they keep repeating the same thing: that condition1 is the rightmost expression, but couldn't understand how. Or how and why condition1 is the first one to get evaluated, irrespective of right or left associativity.
(I'll assume you're referring to the conditional operator in the C family of programming languages. Tag your question with a specific language if you want to focus on that language.)
Case 1: Base case
Let's start with the simplest case, a single conditional expression:
To evaluate this expression, follow these steps:
condition.expression2and evaluateexpression1.expression1and evaluateexpression2.expression1) or Step 3 (the value ofexpression2), whichever applies.Notice that the order of evaluation goes from left to right:
conditionis evaluated first, followed by eitherexpression1orexpression2.Case 2:
conditionis a conditional expressionIf the
conditionincondition ? expression1 : expression2is itself a conditional expression, then you get the following overall expression:To evaluate this expression, follow the same steps as in the base case but replace
conditionwithsubcondition ? subexpression1 : subexpression2. In Step 1, you'll recurse:subcondition ? subexpression1 : subexpression2. To do this, follow the same steps as in the base case:subcondition.subexpression2and evaluatesubexpression1.subexpression1and evaluatesubexpression2.subexpression1) or Step 1.3 (the value ofsubexpression2), whichever applies.expression2and evaluateexpression1.expression1and evaluateexpression2.expression1) or Step 3 (the value ofexpression2), whichever applies.Notice that the order of evaluation goes from left to right:
subconditionis evaluated first, followed by eithersubexpression1orsubexpression2, followed by eitherexpression1orexpression2.Case 3:
expression2is a conditional expressionIf the
expression2incondition ? expression1 : expression2is itself a conditional expression, then you get the following overall expression:To evaluate this expression, follow the same steps as in the base case but replace
expression2withsubcondition ? subexpression1 : subexpression2. In Step 3, you'll recurse:condition.subcondition ? subexpression1 : subexpression2and evaluateexpression1.expression1and evaluatesubcondition ? subexpression1 : subexpression2. To do this, follow the same steps as in the base case:subcondition.subexpression2and evaluatesubexpression1.subexpression1and evaluatesubexpression2.subexpression1) or Step 3.3 (the value ofsubexpression2), whichever applies.expression1) or Step 3 (the value ofsubcondition ? subexpression1 : subexpression2), whichever applies.Notice that the order of evaluation goes from left to right:
conditionis evaluated first, followed by eitherexpression1orsubcondition, followed by (in thesubconditioncase only) eithersubexpression1orsubexpression2.Left versus right associativity
Now let's look at your original expression, but omitting all parentheses:
This could potentially be interpreted in two different ways:
(condition1 ? expression1 : condition2) ? expression2 : expression3condition1 ? expression1 : (condition2 ? expression2 : expression3)An important thing to understand is that associativity and order of evaluation are two completely different concepts. For the conditional operator, left or right associativity simply determines whether you're in Case 2 or Case 3. But in both cases, the order of evaluation goes from left to right, with
condition1evaluated first.