Boolean parameter with check up

Posted on 2014-11-26
Last Modified: 2014-11-28
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?
Question by:Ingo Foerster
  • 4
  • 3
LVL 86

Expert Comment

ID: 40467550
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.

LVL 86

Expert Comment

ID: 40467690
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.

Author Comment

by:Ingo Foerster
ID: 40467795
So as I understand, if I have a boolean

x = upsampleFPS ==true ? false

So when upsampleFPS is true it will be true else false?
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 86

Expert Comment

ID: 40467876
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'.

Author Comment

by:Ingo Foerster
ID: 40467890
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?
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 40467915
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


Author Closing Comment

by:Ingo Foerster
ID: 40470125
Thank you.

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now