# Boolean parameter with check up

Hi,
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
###### 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.

Commented:
This is called the 'ternary operator', you will find a clear and concise description at http://www.cprogramming.com/reference/operators/ternary-operator.html or http://www.cplusplus.com/articles/1AUq5Di1/ (as well as http://en.wikipedia.org/wiki/%3F: - WP covers other languages as well. The scoop is

``````(expression 1) ? expression 2 : expression 3
``````

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

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

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

``````msFrameSpan /= upsampleFPS ? 2 : 1;
``````

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;
``````
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.
ProgrammerAuthor 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?
Commented:
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'.
ProgrammerAuthor 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?
Commented:
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;
``````

Experts Exchange Solution brought to you by