A Demonstration of the Conditional Operator : tenary operator « Operators statements « C++ Tutorial






#include <iostream>
using namespace std;
int main()
{
   int x, y, z;
   cout << "Enter two numbers.\n";
   cout << "First: ";
   cin >> x;
   cout << "\nSecond: ";
   cin >> y;
   cout << "\n";

   if (x > y)
      z = x;
   else
      z = y;

   cout << "After if test, z: " << z;
   cout << "\n";

   z =  (x > y) ? x : y;

   cout << "After conditional test, z: " << z;
   cout << "\n";
   return 0;
}








3.7.tenary operator
3.7.1.Use the ? operator to prevent a division by zero
3.7.2.Using the conditional operator to select output
3.7.3.A Demonstration of the Conditional Operator
3.7.4.Need parentheses around the conditional expression.