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

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

4

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;
5
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest