Syntax question "? a : b;"?

In the following code below.

1
2
3
4
5
6
7
Template <typename T>
T maximum(T a, T b) {
  T result;
  result = (a > b) ? a : b;
  return result;
}


On the line where it says:

result = (a > b) ? a : b;

what exactly does that mean?
You're referring to the ternary operator.
Wikipedia has a page for it: https://en.wikipedia.org/wiki/%3F:

See: https://en.wikipedia.org/wiki/%3F:#Conditional_assignment

The format of the operation is like this:
1
2
3

 variable = condition ? value_if_true : value_if_false 


In your example, it is equivalent to doing:
1
2
3
4
5
T result;
if (a > b)
    result = a;
else
    result = b;
Last edited on
Topic archived. No new replies allowed.