Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
I’ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator.
The conditional looks as follow:
condition-test ? first-expression : second-expression
If the condition-test is nonzero, the first-expression is evaluated, otherwise the second-expression is evaluated.
// Traditional approach
if (x > 0)
str = @"positive";
else
str = @"negative";
// Conditional operator
str = x > 0 ? @"positive" : @"negative";
Nesting Conditional Operators
Extending the example above, we can update the code for the case where x is neither positive or negative (x is zero), using nested conditionals:
// Traditional approach
if (x > 0)
str = @"positive";
else if (x < 0)
str = @"negative";
else
str = @"zero";
// Conditional operator
str = x > 0 ? @"positive" : x < 0 ? @"negative" : @"zero";
As shown above, based on the association of parameters, no parenthesis are necessary, however, I find this much easier to read:
str = x > 0 ? @"positive" : (x < 0 ? @"negative" : @"zero");
Good Use Case
I find the conditional operator useful when creating messages in which part of the message may be plural or singular depending on the value of a variable.
It’s not much more work to make messages appear as expected, for example, how often have you seen a message from an application that looks similar to this “1 file(s) downloaded” where ideally the message would read “1 file downloaded” – not a big deal, however, it’s a trivial amount of code to get this right:
int x = 1;
NSString *str =
[NSString stringWithFormat:@"%d file%s downloaded", x, (x > 1 ? "s" : "")];
Sometimes it’s the little things that will setup you apart in a crowd of developers.
Related posts:
- Conditional Compilation
Comments
5 Responses to “C Conditional Operator aka Ternary Operator”
Leave Comment
I like the ternary operator for two reasons. First, it’s a good way to do conditional assignment on the same line as the definition of the variable that gets the result. Second, it’s a good way to cut down on curly brackets, as an alternative to the horrid practice of nesting a single conditional line below the condition without brackets. The one drawback is the increased difficulty of inserting breakpoints on the individual conditions.
[Reply]
Love this operator as well! It’s in fact the only operator with three operands (hence the name :-). Some might argue that they don’t use it for code-readability’s sake but if you know what the operator does you can figure out the code just as quick as an if-else expression. I wouldn’t nest them however.
Regards,
Steff
[Reply]
Nice post! Filing as “ternary refresher”.
[Reply]
It’s a favorite of mine too, and I often use it for more complicated assignments that would otherwise require multiple if-else expressions. I prefer this layout for easy reading:
str = x > 0 ? @”positive”
: x < 0 ? @"negative"
: @"zero";
[Reply]
tell me detail about ++/– and ternary(?:) operator in c++ with example
eg. Q) a=++b + ++b , if b=10 initialy.
Q2.) write a c++ prg to input type student ‘A’ or ‘B’.If student type is ‘A’ initializ college account=200/ otherwise initialized hostal account=200.
[Reply]