What does “?” and “:” do in boolean statements? [duplicate]

霸气de小男生 提交于 2019-12-02 04:18:38

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.

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);
}
condition ? first statement : second statement

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

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!

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......

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!