Jump to content
  • Advertisement
Sign in to follow this  
Chad Smith

C++ Conditional Operator

This topic is 2071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've always heard and known of the conditional operator in C++ though for some reason my brain would always forget exactly how it's read.  I decided to take a quick look at it again as I do see some places in my code that is a lot longer than it should be just because I always used a full if/else statement to do one quick test. I just wanted to ask to make sure I am reading these right.

 

lets say I have the following:

// assume x has already been declared and defined earlier in the program.
int a = (x > 100) ? 0 : 1;

 

This would read and be the same as:

// again assume x is defined and declared earlier in the program
if(x > 100)
     a = 0;
else
     a = 1;

 

I just want to make sure I am right in reading this.  Even though I do feel dumb that I have always forgot about this operator.

 

So in other words really if the first expression in true, then the second expression is executed.  If the first expression is false then the third expression is executed.  Right?

Edited by Chad Smith

Share this post


Link to post
Share on other sites
Advertisement
It is called the “ternary operator”, in case you want to research it more on Google.  “Conditional operator” is just a common name for it.

 

 

L. Spiro

I think conditional operator is the correct term, and "ternary operator" is a common name for it. "ternary operator" just means the operator takes three operands, like how a "unary operator" takes one (!, -, *, etc), and how a "binary operator" takes two (+, -, *, /, etc).

Share this post


Link to post
Share on other sites

C# also has the null coalesce operator ??.

Foo x = a ?? b;

 

That is, x is assigned a if it's not null, and b otherwise. Reading wikipedia I realize that with GNU extensions, C/C++ also has that operator.

Foo* x = a ?: b;

 

Wow, the more you learn, the more you realize how little you know :)

Share this post


Link to post
Share on other sites

While I'm not advocating this as a good idea, the ternary operator is, as ultramailman said, an expression not a statement. This allows for some interesting things don't have a "simple" translation into if/else statements.

 

For example, choosing a variable to assign:

int a = std::rand(); int b = std::rand(); std::cout << "a: " << a << ", b: " << b << '\n';   (a > b ? a : b) = 42; std::cout << "a: " << a << ", b: " << b << '\n';

 

Or even choosing between functions, but calling them with a single set of arguments:

void foo(int x) {     std::cout << "Foo: " << x << "\n"; }   void bar(int x) {     std::cout << "Bar: "<< x << "\n"; }   void example(int x) {     (x > 100 ? foo : bar)(x); }

 

To reiterate, just because these are possible does not make them a good idea!

Share this post


Link to post
Share on other sites
I often use ?: in variable initializations or in return statement, because it's easier to see that I am doing only one thing.
bool ChessBoard::is_in_check(int color) const {
  int king = color == White ? WhiteKing : BlackKing;
  // ...
}

Share this post


Link to post
Share on other sites
Yea I understand the "just because you can use it doesn't make it right" argument. I don't plan to use it exclusively. Though their were some small areas in my code that just looked weird using a full if/else statement when just initializing a simple variable. After changing some code to it I do seem to like this way better than my if/else statements.

Thanks everyone.

Share this post


Link to post
Share on other sites
I think conditional operator is the correct term, and "ternary operator" is a common name for it.
The term that C++ standard uses for it is conditional operator. See section 5.16 in all of C++98, C++03 and C++11.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!