Do nothing in the use of ternary operator


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

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?


if condition
  n = 1

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.

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

1 Like

Oh yes. It works. Thanks!

condition && ( n = 1 )

is equivalent to the if statement.

n = ifelse( condition, 1, n )

is fast because avoids branching instructions to cpu.

n |= condition

is good when n is 0/1 or Bool.