Conditional operator: ?

This example reads in two numbers and finds the smallest and largest. Notice that there are two assignments for each condition. There are separated by a comma.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Find smallest and largest
#include <iostream>
using namespace std;
int main() {
  double num1, num2, small, large;
  cout << "Enter a number:";
  cin >> num1;
  cout << "Enter another number:";
  cin >> num2;
  (num1 < num2) ? (small = num1, large = num2) : (small = num2, large = num1);
  cout << "smallest is " << small << endl;
  cout << "largest is " << large << endl;
  system("pause");
  return 0;
} // main
CODE

NEXT: Calculate tip using conditional