I want to use ternary operator without else in C. How do I do it.
(a)? b: nothing;
something like this. What do I use in nothing part?
I want to use ternary operator without else in C. How do I do it.
(a)? b: nothing;
something like this. What do I use in nothing part?
If you are using a ternary operator like that, presumably it could be replaced by:
if (a) { b; }
which is much, much better. (The intent is clearer, so the code is easier to read, and there will be no performance loss.)
However, if you are using the ternary operator as an expression, i.e.
printf("%d cat%s", number_of_cats, number_of_cats != 1 ? "s" : <nothing>);
a = b*c + (d == 0 ? 1 : <nothing>);
then the <nothing>
value depends on the context it is being used in. In my first example, <nothing>
should be ""
, and in the second it should be 0
.
An omitted false expression is invalid. Try reversing the condition instead.
(!a) ?: b;
if-else
is a control flow construct wheras ?:
is an operator, and x ? y : z
is an expression - an expression cannot have "no value", while control flow can have a "do nothing path". There is no real equivalence between if-else and ?: - they are not interchangeable in all circumstances.
You can achieve the effect you want in some circumstances, but it is probably less efficient that using if
without else
. For example the effect of:
if( x )
{
y = z ;
}
can be achieved by:
y = x ? z : y ;
but there is an effective but redundant else y = y
assignment when x is false which your compiler may or may not optimise out. So you can achieve the effect but only if you know the "current value" to return as the ?:
expression result.
You can't omit the else
part. Just use a none expression.
But, in that case, it is often better to use an if
...
Seems this question has been around for a while, but FWIW a short C program compiled with GCC 4.6.3 revealed the following:
a = a ? b:;
. "error: expected expression before ‘;’ token".a = a ?: b;
and a = a ? a : b;
Perhaps someone can add more compiler-side details, but to me it seems that omitting the true
execution path is just being fancy.
I tried putting any integers there and it works well. For example, if you want to return "a" if it's evaluated true, then you can write it like:
true ? a : 1;
I did this
isLeapYear(i) ? cout<< i<<endl: cout<<"" ;
Not sure though, if there is any performance hit with this.
Ternary expressions base themselves on deriving statements from boolean conditions. If a statement is marked void()
then as you expect, nothing happens.
C++ code:
condition ? add(value) : void();
C code:
condition ? add(value) : 0;
If I knew that expression must be true, otherwise program is in invalid state, but ternary operator fits better than regular if()
statement, I used to do like this
condition ? value : throw;
And leave it as above or add specific exception.
There are a variety of other forms a ? b : nothing;
can be take.
Short circuit evaluation where b
will only be evaluated if a
is true:
a && b;
Ternary where the nothing
expression is (void)0
:
a ? b : (void)0;
Reversed ternary condition with a blank middle (may not be portable):
!a ? : b;
A simple if
statement:
if (a) {
b;
}