Ingo Foerster
asked on
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
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
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
Or, to explain that using your own code snippet as an example:
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
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.
ASKER
So as I understand, if I have a boolean
x = upsampleFPS ==true ? false
So when upsampleFPS is true it will be true else false?
x = upsampleFPS ==true ? false
So when upsampleFPS is true it will be true else false?
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'.
ASKER
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?
So bool x = upsampleFPS==25 ? true: false
So if upsampleFPS is 25 it will set x to true else to false?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.