What's this? .Pos.Y += (up) ? step : -step;

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
jfisher446
Posts: 11
Joined: Mon Mar 14, 2011 10:48 am

What's this? .Pos.Y += (up) ? step : -step;

Post by jfisher446 »

...[index].Pos.Y += (up) ? step : -step;

General question here. I have NO idea how I'd begin to look this up so if someone could explain the (up), ?, and : portions of this that would be great. up is a bool that's passed in, but why's it in () and how's it used to perform what seems like a math calculation, and I've never encountered the ? or : used like this before. step is how many units to move a vertex I know but the rest is confusing the hell out of me.

Edit: wait. is it sort of an inline if-then-else statement? if up is true, then use a positive step value, otherwise (:) use a negative one?
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: What's this? .Pos.Y += (up) ? step : -step;

Post by REDDemon »

you can write it also as:

Code: Select all


if(up)
{
    [index].Pos.Y +=step;
}
else
{
    [index].Pos.Y -= step;
}
yes it is a if-than-else statement, sometimes is usefull using it. usually is not used very often.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Brainsaw
Posts: 1183
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I like this statement. I often use if for debug printing, e.g.

Code: Select all

printf("some string=\"%s\"\n",someString!=NULL?someString:"NULL");
or in return statements, e.g. in getter methods

Code: Select all

myObject *getObjectAtIndex(u32 index) {
  return index<objectArray.size()?objectArray[index]:NULL;
}
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
jfisher446
Posts: 11
Joined: Mon Mar 14, 2011 10:48 am

Post by jfisher446 »

Wow, thanks guys. Never would have thought to look up ternary operations and probably a longer shot searching wikipedia for '?'... I appreciate the help. And thanks Brainsaw for the tip, I can see the usefulness
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

If i'm not mistaken, it's also possible to use it like this

Code: Select all

 int x=5, y=6;
 bool storeinx=true;
 (storeinx?x:y) = 4;
I cant access codepad atm so i cant check it. If i remember correctly one can get some amusing behaviours from different compilers when using the ternary operator like this.
Post Reply