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.)
Let's start with the simplest case, a single conditional expression:
condition ? expression1 : expression2
To evaluate this expression, follow these steps:
condition
.expression2
and evaluate expression1
.expression1
and evaluate expression2
.expression1
) or Step 3 (the value of expression2
), whichever applies.Notice that the order of evaluation goes from left to right: condition
is evaluated first, followed by either expression1
or expression2
.
condition
is a conditional expressionIf the condition
in condition ? expression1 : expression2
is itself a conditional expression, then you get the following overall expression:
(subcondition ? subexpression1 : subexpression2) ? expression1 : expression2
To evaluate this expression, follow the same steps as in the base case but replace condition
with subcondition ? subexpression1 : subexpression2
. In Step 1, you'll recurse:
subcondition ? subexpression1 : subexpression2
. To do this, follow the same steps as in the base case:
subcondition
.subexpression2
and evaluate subexpression1
.subexpression1
and evaluate subexpression2
.subexpression1
) or Step 1.3 (the value of subexpression2
), whichever applies.expression2
and evaluate expression1
.expression1
and evaluate expression2
.expression1
) or Step 3 (the value of expression2
), whichever applies.Notice that the order of evaluation goes from left to right: subcondition
is evaluated first, followed by either subexpression1
or subexpression2
, followed by either expression1
or expression2
.
expression2
is a conditional expressionIf the expression2
in condition ? expression1 : expression2
is itself a conditional expression, then you get the following overall expression:
condition ? expression1 : (subcondition ? subexpression1 : subexpression2)
To evaluate this expression, follow the same steps as in the base case but replace expression2
with subcondition ? subexpression1 : subexpression2
. In Step 3, you'll recurse:
condition
.subcondition ? subexpression1 : subexpression2
and evaluate expression1
.expression1
and evaluate subcondition ? subexpression1 : subexpression2
. To do this, follow the same steps as in the base case:
subcondition
.subexpression2
and evaluate subexpression1
.subexpression1
and evaluate subexpression2
.subexpression1
) or Step 3.3 (the value of subexpression2
), whichever applies.expression1
) or Step 3 (the value of subcondition ? subexpression1 : subexpression2
), whichever applies.Notice that the order of evaluation goes from left to right: condition
is evaluated first, followed by either expression1
or subcondition
, followed by (in the subcondition
case only) either subexpression1
or subexpression2
.
Now let's look at your original expression, but omitting all parentheses:
condition1 ? expression1 : condition2 ? expression2 : expression3
This could potentially be interpreted in two different ways:
(condition1 ? expression1 : condition2) ? expression2 : expression3
condition1 ? 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 condition1
evaluated first.