Using the conditional operator to select output : tenary operator « Operators statements « C++ Tutorial






#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
  int mice = 1;
  cout << "You have "
       << mice
       << (mice == 1 ? " mouse " : " mice ")
       << "in total."
       << endl;
  return 0;
}
You have 1 mouse in total.








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.