1

I think this question is a general programming question, but let's assume I'm asking this for Java.

what does the following statement do ?

return a ? (b || c) : (b && c);

I have seen the syntax with ?'s and :'s in many topics at SO, this particular one I found in Check if at least two out of three booleans are true

But I don't know what they mean, so how to use them, and I believe it's something very useful for me.

Thanks !

Community
  • 1
  • 1
user223150
  • 125
  • 5
  • It's a short cut `if-else` statement – MadProgrammer Jun 04 '13 at 05:26
  • `? :` does the same thing returning `boolean` values as it does with any other type. – Peter Lawrey Jun 04 '13 at 05:28
  • So why does everyone here think this is a duplicate of that question? That question has little to do with this one. – Makoto Dec 27 '13 at 22:52
  • @Makoto Should have been marked as a duplicate of [What is the Java ?: operator called and what does it do?](http://stackoverflow.com/q/798545/479863) but I don't think it is worth reopening just so that it can be closed as a duplicate of a different question. – mu is too short Dec 27 '13 at 23:18

6 Answers6

7

That's the conditional operator. It means something like:

condition ? value-if-true : value-if-false;

So in your case, it returns b || c if a is true, and b && c if a is false.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Thank you so much! I cannot believe I couldn't find such a simple thing in google, and I was right, it saves at least 4 lines of code for me :) – user223150 Jun 04 '13 at 05:27
  • It's technically called the conditional operator - and it happens to be *a* ternary operator (takes three arguments). – Blorgbeard Jun 04 '13 at 05:28
  • And, are ternary operators valid for common languages like C,PHP,JavaScript etc? I will choose your answer in 10 minutes. – user223150 Jun 04 '13 at 05:28
  • Yes, as far as I know it originated in C. – Carl Norum Jun 04 '13 at 05:28
  • Wikipedia says it comes from CPL. And they call it the ternary operator, too. I don't know where to look up an official name. Is there a java spec out there somewhere? C definitely calls it the conditional operator. – Carl Norum Jun 04 '13 at 05:35
  • @CarlNorum Is it possible to do two things if condition is true or false with `ternary ops`? for eg. `condition ? do this and do that : do something else and something even more` – jaypal singh Jun 04 '13 at 05:39
  • I don't know enough java to know if it has a comma operator. Yes, you can probably do more than one thing, though. Why not just use `if`/`else` in that case? – Carl Norum Jun 04 '13 at 05:41
3

This is known as a ternary statement; it's shorthand for an if-else block - you can google that for more info.

Your example is equivalent to

if (a) {
   return (b || c);
} else {
   return (b && c);
}
johnd
  • 516
  • 5
  • 9
  • Yes, I was looking for this for days. I'm always trying to simplify my code and this operator turns these 5 lines to 1 line. I love it ! Thanks. – user223150 Jun 04 '13 at 05:31
  • @user223150 Less lines does not mean simpler code, really. The if/else-clause is simpler and more readable if you ask me. Putting ternary statements in ternary statements can become a messy ordeal. – ddmps Jun 04 '13 at 05:44
2
condition ? first statement : second statement

if condition is true then first statement is executed otherwise the second statement

0

It's the ternary operator, the whole statement expands to something more like this:

if a == true then
  if b == true or c == true then
    return true
else 
  if b == true and c == true then
    return true

As your link says a much more elegant way to check if at least 2 out of three booleans are true when applied in this way!

0

its an conditional operator... jst like if and else....

e.g----

a<b ? 4 :5      where a= 2 and b=5

as a is less then b.... then this operator will return 4... else it return 5....

in short... if your condition i.e statement before ? is correct then it returns 1st value.. i.e statement before colon.... else it returns 2nd value......

0

According to your code, return a ? (b || c) : (b && c);

Result will be like this :

if a == true , then result = b || c otherwise result = b && c

its a ternary operator & used in most of the languages C,C++, java, Javascript

John
  • 145
  • 1
  • 9