Boolean parameter with check up

I try to find out how the logic of this code is: msFrameSpan /= upsampleFPS ? 2 : 1;
So I want to learn how to use the questionmark to give a boolena parameter. Where I can find information about this?
Ingo FoersterProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is called the 'ternary operator', you will find a clear and concise description at or (as well as - WP covers other languages as well. The scoop is

(expression 1) ? expression 2 : expression 3

Open in new window

If expression 1 evaluates to true, then expression 2 is evaluated.

If expression 1 evaluates to false, then expression 3 is evaluated instead.

Or, to explain that using your own code snippet as an example:

msFrameSpan /= upsampleFPS ? 2 : 1;

Open in new window

If 'upsampleFPS is non-zero, 'msFrameSpan' will be divided by 2, otherwise it will be divided by 1 - i.e. stay the same. Which also illustrates that this operator can be misused, since a
 if (upsampleFPS) msFrameSpan /= 2;

Open in new window

would achieve the same, omitting an unnecessary division by 1. Not sure if every compiler is able to optimize that code to leave out that operation.
Ingo FoersterProgrammerAuthor Commented:
So as I understand, if I have a boolean

x = upsampleFPS ==true ? false

So when upsampleFPS is true it will be true else false?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

That will depend on what type 'upsampleFPS' is. If it is 'bool', 'true' or 'false' are the two options, but an expression in the sense of C/C++ is considered 'true' when it is non-null. 'false' is the equivalent to 'equal to 0'.
Ingo FoersterProgrammerAuthor Commented:
upsampleFPS is a simple integer. It contain pal or ntsc.  

So bool x = upsampleFPS==25 ? true: false

So if upsampleFPS is 25 it will set x to true else to false?
Yes, that's it. To make the above a bit more readable, I'd add parenthesis like

bool x = upsampleFPS==25 ? true: false;
// or, just a bit nicer to read
bool x = (upsampleFPS==25) ? true: false;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ingo FoersterProgrammerAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.