views:

1214

answers:

6
int qempty(){ return(f==r)?1:0;}

In the above snippet, what does "?" mean? What can we replace it with?

+19  A: 

This is commonly referred to as the conditional operator, and when used like this:

condition ? result_if_true : result_if_false

... if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false.

It is syntactic sugar, and in this case, it can be replaced with

int qempty()
{ 
  if(f == r)
  {
      return 1;
  } 
  else 
  {
      return 0;
  }
}

Note: Some people refer to ?: it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.

Daniel LeCheminant
I think they implicitly mean "the (C++) ternary operator". What other ternary operators exist in C++?
sblom
That's the only one in C++
Colin
For anyone in the need of conditional love: http://www.artima.com/cppsource/foreach.html . eric niebler explains how his boost.foreach uses the conditional operator.
Johannes Schaub - litb
In regular code, it's syntactic sugar, but it does enable you to do conditional initialization in the initialization list of the constructot.
JohnMcG
@JohnMcG wait, what? Could you give an example?
Tim Snowhite
Foo(Bar* y) pMember (y == NULL ? NULL : y->pMember) -- Here, we initialize pMember to y's pMember, or NULL if it's not there. Can't put if-else in a constructor initialization, so ternary operator makes it possible.
JohnMcG
+2  A: 

It's the conditional operator.

a ? b : c

It's a shortcut for IF/THEN/ELSE.

means: if a is true, return b, else return c. In this case, if f==r, return 1, else return 0.

Joe
+5  A: 

This is a ternary operator, it's basically an inline if statement

x ? y : z

works like

if(x) y else z

except, instead of statements you have expressions; so you can use it in the middle of a more complex statement.

It's useful for writing succinct code, but can be overused to create hard to maintain code.

Richard
You have to be careful here; it doesn't work exactly like an if statement. While you *can* say something like int `a = x ? y : z;` you *can't* say `int a = if(x) y else z;`
Daniel LeCheminant
worthwhile to know that there is a sequence point at the '?'. That means the following is valid: ++x ? x : y;
Johannes Schaub - litb
@Daniel, that's what I meant by having expressions rather than statements. I probably wasn't explicit enough about the difference, so thanks for adding some clarification.
Richard
+2  A: 

It is called the conditional operator.

You can replace it with:

int qempty(){ 
    if (f == r) return 1;
    else return 0;
}
jjnguy
A: 

The question mark is the conditional operator. The code means that if f==r then 1 is returned, otherwise, return 0. The code could be rewritten as

int qempty()
{
  if(f==r)
    return 1;
  else
    return 0;
}

which is probably not the cleanest way to do it, but hopefully helps your understanding.

ssakl
+2  A: 

You can just rewrite it as:

int qempty(){ return(f==r);}

Which does the same thing as said in the other answers.