Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / C++ Boolean Assignments

Author
Message
heyufool1
16
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 23rd Jan 2011 19:21
Hi! I'm making a game and editor in C++ but I came across this today:

I'm assuming what this really is, is:

I can't find any information on this using google so can anyone clarify this for me? Also, I rarely see this method used so is it frowned upon for any reason? Thanks!

"So hold your head up high and know, it's not the end of the road"
Impulse Game Engine
BiggAdd
Retired Moderator
20
Years of Service
User Offline
Joined: 6th Aug 2004
Location: != null
Posted: 23rd Jan 2011 19:25 Edited at: 23rd Jan 2011 19:29
Its called a Ternary Operation/Operator:

http://en.wikipedia.org/wiki/Ternary_operation

And yes you are right, it does the same as your code snippit.
Quote: "
I can't find any information on this using google so can anyone clarify this for me? Also, I rarely see this method used so is it frowned upon for any reason? Thanks!"



Its just used as a neater way of assigning a variable under a condition. Its perfectly valid.

heyufool1
16
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 23rd Jan 2011 19:31
Ahh, cool. Learn something everyday thanks for the info!

"So hold your head up high and know, it's not the end of the road"
Impulse Game Engine
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 23rd Jan 2011 22:49
I love the ternary operator!!!!

I remember, my teacher wanted us to finish this:
http://codingbat.com/prob/p183562
in the shortest number of lines. The Challenge is as follows:

my solution:



If you use underscores for variables and ternary operators a lot, your code can be annoyingly hard to read:
(_==__?_*__:__/_)<___?_:__


heyufool1
16
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 24th Jan 2011 00:45 Edited at: 24th Jan 2011 00:48
Quote: "return ((((big>(goal/5))?goal/5:big)*5+small)>=goal);"

nice, did you start with that or did you go from a full decision structure down to that? Just curious because it's hard for me to follow

"So hold your head up high and know, it's not the end of the road"
Impulse Game Engine
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 24th Jan 2011 05:06
Quote: "nice, did you start with that or did you go from a full decision structure down to that? Just curious because it's hard for me to follow "

Originally I was gonna type three lines

int blocks=((big>(goal/5))?goal/5:big)*5+small
if goal<=blocks then return true;
return false

But while typing the second I realized the last two lines are redundant. It's checking if a boolean is true or false and returning a boolean, when you could just return that boolean >.>
Basically, the first ternary returns the largest number of "big bricks" that can fit into "goal". If there are more bigBricks than can fit into Goal, then goal/5 rounded down will be the number of bigBricks (integer division rounds down). That number times five is the length in inches, and since small bricks are the smallest resolution, if this number plus the length of the small bricks is greater or equal to the total length, we can fit everything together.


The ternary operator can make simple logic look complicated, BUT also more compact and neat. Generally, if I have to nest more than two or three ternary statements, I just nest else/ifs.

Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 26th Jan 2011 04:34
I love and use ternary operators every day at work in PHP. Just saying


Senior Web Developer - Nokia
heyufool1
16
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 26th Jan 2011 15:48
Well, it seems that it's a good thing to know

"So hold your head up high and know, it's not the end of the road"
Impulse Game Engine

Login to post a reply

Server time is: 2025-05-22 11:56:28
Your offset time is: 2025-05-22 11:56:28