Sunday 31 January 2016

java - What does "?" and ":" do in boolean statements?




I think this question is a general programming question,

but let's assume I'm asking this for Java.



what does the following statement do ?



return a ? (b || c) : (b && c);


I have seen the syntax with ?'s and :'s in many topics at SO, this particular one I found in Check if at least two out of three booleans are true



But I don't know what they mean, so how to use them, and I believe it's something very useful for me.




Thanks !


Answer



That's the conditional operator. It means something like:



condition ? value-if-true : value-if-false;


So in your case, it returns b || c if a is true, and b && c if a is false.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...