Note: It has become apparent that what is known as the ternary operator in C is in fact called the "Conditional Operator" in C++. Thank you, Grey Wolf.
So I was reading some code and I saw something that I'd never seen before:
(x == y) ? a : b
I pried open my C book (K&R) to find out what it was. "Ternary Operator" it said.
Some people might not know how to use it, so I thought I'd write a simple explanation:
Basic Syntax:
The ternary operator (?:) is a very useful conditional expression used in C and C++. It's effects are similar to the if statement but with some major advantages.
The basic syntax of using the ternary operator is thus:
(condition) ? (if_true) : (if_false)
Which is basically the same as:
1 2 3 4
|
if (condition)
if_true;
else
if_false;
|
Therefore if "condition" is true, the second expression is executed ("if_true"), if not, the third is executed ("if_false").
Now take the following example:
1 2 3 4 5 6 7
|
if (a > b) {
largest = a;
} else if (b > a) {
largest = b;
} else /* a == b */ {
std::cout << "Uh oh, they're the same!\n";
}
|
That requires 7 lines and is unsuitable for something like a MIN/MAX function:
1 2
|
#define MIN(a, b) ((a < b) ? a : b)
#define MAX(a, b) ((a > b) ? a : b)
|
The above code can be used like this:
int largest = MAX(a, b);
and will correctly assign a or b to largest.
Instead of the lengthy and more annoying (in this case) if statement we can use the ternary operator:
largest = ((a > b) ? a : b);
The main fundamental difference is that the ternary operator is an
expression whereas the if structure is a
statement. A ternary operator expression's result can be assigned to a variable -- the results of an if statement cannot.
Obviously the only advantage really that the if statement has is that you can have multiple clauses, where as with the ternary operator you can have only two.
However, according to wikipedia:
A GNU extension to C allows the second operand to be omitted, and the first operand is implicitly used as the second as well:
a = x ? : y;
The expression is equivalent to
a = x ? x : y;
except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression has side effects. |
If you're still unsure, try running the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
int main() {
int a, b;
a = b = 0;
std::cout << "Enter a number: ";
std::cin >> a;
std::cout << "\nEnter a number: ";
std::cin >> b;
std::cout << "Largest: " << ((a > b) ? a : b) << "\nSmallest: " << ((a < b) ? a : b) << std::endl;
return 0;
}
|
Output:
$ g++ "MIN MAX.cpp" -o mm
$ ./mm
Enter a number: 256
Enter a number: 652
Largest: 652
Smallest: 256
|
Note: Stuff in bold is my input.
Please feedback :)
Sources:
The C Programming Language -- Brian Kernighan and Dennis Ritchie
http://en.wikipedia.org/wiki/Conditional_operator
Edits:
1. Fixed error noted by kempofighter,
2. Fixed errors in MIN & MAX macro functions where a & b were declared as A & B;
3. Fixed missing bracket in MAX(a, b) macro.