Solved

Boolean parameter with check up

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

Expert Comment

by:jkr
ID: 40467550
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

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.

0
 
LVL 86

Expert Comment

by:jkr
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.
0
 

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?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 86

Expert Comment

by:jkr
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'.
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?
0
 
LVL 86

Accepted Solution

by:
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

0
 

Author Closing Comment

by:Ingo Foerster
ID: 40470125
Thank you.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
VS2015 compilation and missing DLLs 9 161
How can i compile this github project?? 2 90
C++ mouse_event mouse look 7 96
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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