# Do nothing in the use of ternary operator

**URL:** <https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486>\
**Category:** New to Julia\
**Tags:** control-flow\
**Created:** [November 19, 2022, 3:57am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486 "2022-11-19T03:57:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [November 19, 2022, 3:57am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/1 "2022-11-19T03:57:54Z")

</div>

Hi,

I am thinking about a simple question. Suppose I have three numbers `a`, `b` and `c`. And I want to check if the sum of them is zero or not. And I have an object `n=0`. If the sum is not zero, then I want to set another object `n=1`, if the sum is zero then I do nothing. To achieve this, I have a sentence

```julia
a+b+c!=0 ? n=1 : n=0

```

But I think the part of the colon `：` is redundant, is there a way to ignore it?

Thanks

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 19, 2022, 4:00am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/2 "2022-11-19T04:00:40Z")

</div>

```julia
if condition
  n = 1
end

```

---

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [November 19, 2022, 4:03am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/3 "2022-11-19T04:03:27Z")

</div>

Yes, I agree that simply using `if` can solve this problem. But I still wonder if there is a different way to solve this problem.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 19, 2022, 4:03am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/4 "2022-11-19T04:03:47Z")

</div>

`n = condition ? 1 : n` is a way without `if`.

---

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [November 19, 2022, 4:07am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/5 "2022-11-19T04:07:29Z")

</div>

Oh yes. It works. Thanks!

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 19, 2022, 4:16am UTC](https://discourse.julialang.org/t/do-nothing-in-the-use-of-ternary-operator/90486/6 "2022-11-19T04:16:23Z")

</div>

```julia
condition && ( n = 1 )

```

is equivalent to the `if` statement.

```julia
n = ifelse( condition, 1, n )

```

is fast because avoids branching instructions to cpu.

```julia
n |= condition

```

is good when `n` is 0/1 or Bool.
