• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Chad Smith

C++ Conditional Operator

14 posts in this topic

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
0

Share this post


Link to post
Share on other sites
Yes. And the whole thing is one big expression, so it can be used anywhere expressions can be used (right value, function arguments, etc).
0

Share this post


Link to post
Share on other sites
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).

0

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 :)

0

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;
  // ...
}

2

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

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

Share this post


Link to post
Share on other sites

[quote]

Yea I understand the "just because you can use it doesn't make it right" argument.

[/quote]

Don't get my post wrong, I actually quite like the conditional operator, in particular for cases like Álvaro demonstrated.

 

My post was demonstrating questionable (but interesting) behaviour, hence the caveat.

0

Share this post


Link to post
Share on other sites

In terms of a compiled binary file, is there any difference between ?: and if/else chains?  Won't the compiler optimize them both down to the same rough assembly?  Or is there a distinct advantage for one of the other in any arena but legibility?

 

(I've never used it in a clever manner, it just saves me space in a long statement to nest the ?: expression instead of declaring a full if/else block, hence my curiosity)

0

Share this post


Link to post
Share on other sites
In terms of a compiled binary file, is there any difference between ?: and if/else chains?  Won't the compiler optimize them both down to the same rough assembly?  Or is there a distinct advantage for one of the other in any arena but legibility?

 

It depends.

 

Many microprocessors support conditional assignment.  On these chipsets the code "x = test ? true : false" can be implemented very cleanly.  The compiler may or may not recognize the pattern of assign,if(test){assign}, but the conditional operation makes it clear.

 

There are many potential ways to optimize if/else chains that depend on exactly how they are used.  Again, the individual compiler may or may not recognize them, and may or may not take the optimization.

 

 

Generally this is a micro-optimization.  Use the conditional operation because it makes your code more maintainable, not because of a potential compiler optimization.

0

Share this post


Link to post
Share on other sites

It's a very compiler specific thing, but I vaguely remember an article having a closer look at the resulting code for ?: compared to if/else. One example was that "x = cond ? 5 : 8" would be turned into "x = 5 + cond * 3", while the compiler wasn't able to do the same for the if/else version.

 

It's far from a good reason to try and use ?: for everything, but a nice example of the kind of thing a modern compiler will do to optimize code (which also makes it a good example of why you as a programmer should not go out of your way to write unreadable code, thinking you can outsmart your compiler when it comes to micro optimizing the small stuff).

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0