# Combining 2 ternary conditions, how to?

**URL:** <https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358>\
**Category:** Numerics\
**Created:** [October 31, 2020, 7:52am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358 "2020-10-31T07:52:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 7:52am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/1 "2020-10-31T07:52:58Z")

</div>

Hi,  
newbie, wonderings 🙂  
1: if it is safe to combine 2 (or more) ternary conditions.  
as in this way : (e\>f) ? “e” : (f\<g) ? “f” : “g” - with : e=2, f=4, g=8  
?  
or if that is not recomended.  
Thanks

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 8:03am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/2 "2020-10-31T08:03:52Z")

</div>

Don’t see anything wrong with that, if that’s the most readable way to present your code go for it.

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:07am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/3 "2020-10-31T08:07:10Z")

</div>

Ok, so when I’m trying to change the last condition \>/\<…it always returns me with “g” (never “f”)…  
So, how would one writte it so that it also returns f when f\<g is true…  
testing in jupyter localhost

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 8:19am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/4 "2020-10-31T08:19:03Z")

</div>

Sorry I’m not sure I’m following you - the last condition is `(f < g)` in your post above, and when you change that to `(f > g)` and you have `f = 4`, `g = 8` then that’s false and hence `"g"` is returned. What output are you expecting?

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:21am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/5 "2020-10-31T08:21:10Z")

</div>

Ooops, my bad, the f value was changed = to 8 (f=g)  
so that is why (I guess) it was always returning me “g” in both case (f\>g) or (f\<g)…wich is somehow interesting to know too…

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:22am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/6 "2020-10-31T08:22:51Z")

</div>

you do not receive an “error/warning” msg if both values are equal, meanwhile you check if one is greater than the other ! it just returns you the last value of the condition I guess…

answer : (e\>f) ? e : (f\<g) ? “f” : “g” (…and f=4)  
f is returned now. which is what I was expecting

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:28am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/7 "2020-10-31T08:28:20Z")

</div>

I guess that is why they mostly advise us to use “\>=” instead of strict “\>” !

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 8:28am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/8 "2020-10-31T08:28:31Z")

</div>

Why would you expect an error or warning if both values are equal? Doing e.g. `4 < 4` is perfectly legal and unambiguously false. Use `<=`, `>=` and `==` to explicitly handle cases where numbers are equal.

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:34am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/9 "2020-10-31T08:34:48Z")

</div>

it does sounds logical to me that 4\>4 could possibly return a warning…  
as for using \> instead o \>=… as of now, i’m just following this online course : [Conditionals | JuliaAcademy](https://juliaacademy.com/courses/375479/lectures/5816186)

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [October 31, 2020, 8:36am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/10 "2020-10-31T08:36:39Z")

</div>

thanks for your time nils ! I guess that is solved by now

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 31, 2020, 8:39am UTC](https://discourse.julialang.org/t/combining-2-ternary-conditions-how-to/49358/11 "2020-10-31T08:39:37Z")

</div>

I think you’ll struggle to find any programming language where `>` or `<` give a warning for equal numbers - it is a pretty unambiguous question (“is number one bigger/smaller than number 2”) with an unambiguous answer. Similarly, you wouldn’t expect `1 < 2` to return something like “it’s still smaller, but pretty close!”

In any case it’s always helpful to consider the documentation, here’s the relevant section: [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons)
