What Does? and: Mean in C++? [Closed]
For Example I Have This Statement Return ( M > N? M: N ); What Does the Question Mark Mean Between M > N and M: N? What Does the Colon in M: N Mean? 5 2...
For example I have this statement
return ( m > n ? m : n );
What does the question mark mean between m > n and m : n?
What does the colon in m : n mean?
Its technical name is the conditional operator, and it's shorthand for if-then;else.
if m > n
then m
else n
or in actual C++ syntax:
if(m > n)
{
return m;
}
else
{
return n;
}
What does the question mark mean between m > n and m : n? What does the colon in m : n mean?
The ? and : are part of the conditional operators syntax. The ? indicates that the left expression should be a boolean evaluation true or false. The : delineates the true and false results. The left-hand of : is the result if the expression evaluates to true and the right-hand of : is the result if the expression is false
This is the ternary operator, it means if m is greater than n then do m otherwise do n, it is equivalent to:
if(m>n)
return m;
else
return n;