| New Reply |
Ternary operator in C |
Share Thread |
| Feb6-13, 11:45 AM | #1 |
|
|
Ternary operator in C
I was wondering if anyone knew if it is possible to construct an if else if with the ternary operator in C.
I know that we can use it for if else, but what if you wanted multiple conditions for else if in your statement? Code:
printf("%d",(a>5)?1:(a<5)?0:10);
//Just a silly example
|
| Feb6-13, 12:24 PM | #2 |
|
|
I think you can do the nested ternary ops but you can't insert arbitrary java code in there. It must be an expression.
You could use a user-defined boolean function for the condition and other functions for the result which in effect does the same thing but its probably harder to read in the long run for short if/else type work. double y = <some-user-boolean-function>(a,b,c) ? <some-user-function-returns-a-double>(a,b,c) : <some-other-user-function-that-returns-a-double(a,b,c); |
| Feb6-13, 02:04 PM | #3 |
|
Mentor
|
Sure. It's just another operator. So,
Code:
// Example 1, various assignments to x via conditional statements.
if (cond1)
x = val1;
else if (cond2)
x = val2;
else if (cond3)
x = val3;
else
x = val4;
// Example 2, various assignments to y via conditional statements.
if (cond1)
if (cond2)
if (cond3)
y = val3;
else
y = val2;
else
y = val1;
else
y = val4;
Code:
// Example 1, as a single assignment statement using multiple ternary operators x = cond1 ? val1 : cond2 ? val2 : cond3 ? val3 : val4; // Example 2, as a single assignment statement using multiple ternary operators y = cond1 ? cond2 ? cond3 ? val3 : val2 : val1 : val4; |
| Feb6-13, 02:19 PM | #4 |
|
Recognitions:
|
Ternary operator in C
Please consider that if you use such expressions in a software firm, it's likely that your colleagues will want to cut off your hands. ;)
|
| Feb6-13, 02:52 PM | #5 |
|
|
|
| Feb6-13, 03:45 PM | #6 |
|
Mentor
|
There are a few cases where would I prefer the ternary operator, even chained. Clear, concise, functional. However, I can't use the ternary operator because I tend to work on projects that forbid its use. Rats! Double rats, as I am the author of the stupid coding standards on many of those projects. (I'm quite aware that quite a few people just can't grok functional programming.) |
| Feb6-13, 05:24 PM | #7 |
|
|
http://www.scala-lang.org/ |
| Feb7-13, 02:25 PM | #8 |
|
Recognitions:
|
Functional programming... isn't that where you program fragments like
Code:
(setq grok (?: cond1 val1 cond2 val2 cond3 val3 val4)) |
| Feb7-13, 07:15 PM | #9 |
|
|
Just be aware that it is immoral to write code that is really tough for others to read. Agree with the "cut off your hands" comments above. :-)
|
| Feb7-13, 07:56 PM | #10 |
|
|
|
| Feb7-13, 08:21 PM | #11 |
|
Recognitions:
|
Code:
(setq grok (?: cond1 val1 cond2 val2 cond3 val3 val4)) Code:
⍋x |
| Feb7-13, 09:38 PM | #12 |
|
Mentor
|
Code:
(dosomethingwith (cond ((cond1) val1) ((cond2) val2) ((cond3) val3) (t val4))) Functional programming requires a rather different outlook on programming. |
| Feb7-13, 09:43 PM | #13 |
|
|
Scala has all kinds of examples like this using closures. A closure is a kind of function that is passed to one of many collection iterators that Scala has in its arsenal of programming constructs. |
| Feb8-13, 05:56 AM | #14 |
|
Mentor
|
C++ also has functional programming features. There's std::sort, for example. C++11 takes C++ much further along the road toward being a functional language. Lambda expressions, bindings, currying, ...
|
| New Reply |
Similar discussions for: Ternary operator in C
|
||||
| Thread | Forum | Replies | ||
| ternary expansion | General Math | 2 | ||
| ternary phase diagram | Materials & Chemical Engineering | 1 | ||
| Ternary phase diagram | Biology, Chemistry & Other Homework | 0 | ||
| A Ternary Cantor Set proof... | Calculus & Beyond Homework | 3 | ||
| 012 ternary logic/dialectic | General Discussion | 4 | ||