# Is there a math expression equivalent to the conditional ternary operator?

Is there a math equivalent of the ternary conditional operator as used in programming?

a = b + (c > 0 ? 1 : 2)


The above means that if $$cc$$ is greater than $$00$$ then $$a=b+1a = b + 1$$, otherwise $$a=b+2a = b + 2$$.

From physics, I’m used to seeing the Kronecker delta,$$δij≡{1ifi=j0else, {\delta}_{ij} \equiv \left\{ \begin{array}{lll} 1 &\text{if} & i=j \\ 0 &\text{else} \end{array} \right. _{,}$$and I think people who work with it find the slightly generalized notation$$δ[condition]≡{1if[condition]0else {\delta}_{\left[\text{condition}\right]} \equiv \left\{ \begin{array}{lll} 1 &\text{if} & \left[\text{condition}\right] \\ 0 &\text{else} \end{array} \right.$$to be pretty natural to them.

So, I tend to use $$δ[condition]\delta_{\left[\text{condition}\right]}$$ for a lot of things. Just seems so simple and well-understood.

Transforms:

1. Basic Kronecker delta:
To write the basic Kronecker delta in terms of the generalized Kronecker delta, it’s just$$δij⇒δi=j. \delta_{ij} \Rightarrow \delta_{i=j} \,.$$It’s almost the same notation, and I think most folks can figure it out pretty easily without needing it explained.

2. Conditional operator:
The “conditional operator” or “ternary operator” for the simple case of ?1:0:$$if (condition){ return 1;}else{ return 0;} ⇒ condition ? 1 : 0 ⇒δ[condition]. \begin{array}{ccccc} \boxed{ \begin{array}{l} \texttt{if}~\left(\texttt{condition}\right) \\ \{ \\ ~~~~\texttt{return 1;} \\ \} \\ \texttt{else} \\ \{ \\ ~~~~\texttt{return 0;} \\ \} \end{array} ~} & \Rightarrow & \boxed{~ \texttt{condition ? 1 : 0} ~} & \Rightarrow & \delta_{\left[\text{condition}\right]} \end{array} _{.}$$Then if you want a non-zero value for the false-case, you’d just add another Kronecker delta, $$δNOT([condition]),\delta_{\operatorname{NOT}\left(\left[\text{condition}\right]\right)} ,$$ e.g. $$δi≠j.\delta_{i \neq j} .$$

3. Indicator function:
@SiongThyeGoh’s answer recommended using indicator function notation. I’d rewrite their example like$$a=b+1+1(−∞,0](c)⏟their example⇒a=b+1+δc∈(−∞,0]⏟direct translation⇒a=b+1+δc≤0⏟cleaner form. \begin{array}{ccccc} \underbrace{a=b+1+\mathbb{1}_{(-\infty, 0]}(c)} _{\text{their example}} & \Rightarrow & \underbrace{a=b+1+ \delta_{c \in \left(-\infty, 0\right]}} _{\text{direct translation}} & \Rightarrow & \underbrace{a=b+1+ \delta_{c \, {\small{\leq}} \, 0}} _{\text{cleaner form}} \end{array} \,.$$

4. Iverson bracket:
Iverson bracket notation, as suggested in @FredH’s answer, is very similar to the generalized Kronekcer delta. For example:$$δi=j ⇒ [i=j]. \delta_{i=j} ~~ \Rightarrow ~~ \left[i = j \right] \,.$$Dropping the $$“ \delta “ \delta "$$ reduces backwards-compatibility withe the basic Kroncker delta, plus it weakens the signal about what the notation means, so it’s probably not as good in general contexts right now. However, Iverson bracket notation should be easier to read and write, so when reinforcing the meaning of the notation isn’t a big issue, it could be preferable.

### Note: “Conditional operator” rather than “ternary operator“.

The conditional operator, condition ? trueValue : falseValue, has 3 arguments, making it an example of a ternary operator. By contrast, most other operators in programming tend to be unary operators (which have 1 argument) or binary operators (which have 2 arguments).

Since the conditional operator is fairly unique in being a ternary operator, it’s often been called “the ternary operator“, leading many to believe that that’s its name. However, “conditional operator” is more specific and should generally be preferred.