Customizable training materials to learn C++

Looking for C++ Tutorial Visit Nano teach c++!
  • C++ Manipulators
    12/07/2014 - 0 Comments
    Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display. Some of the more commonly used manipulators are…
  • C++ Data Types
    11/07/2014 - 0 Comments
    Data types in any of the language means that what are the various type of data the variables can have in that particular language. Information is stored in a computer memory…
Nano teach C++ found result for your search here:

C++ Program to demonstrate the use of Ternary Operator

The conditional operator can often be used instead of the if else statement. Since it is the only operator that requires three operands in c++, It is also called the ternary operator.
X = Y > 5 ? 4 : 8;
If Y is greater than 5 then 4 will be assigned to variable X or else the value 8 will be assigned to X.
Example:
#include<iostream>
#include<iomanip> 
using namespace std;

int main (){

    int first, second;

    cout << "Please enter two integers." << endl;

    cout << "First" << setw (3) << ": ";
    cin >> first;

    cout << "Second" << setw (2) << ": ";
    cin >> second;

    string message = first > second ? "first is greater than second" : "first is less than or equal to second";

    cout << message << endl;
    system("pause");
    return 0; 

}
or
string message = first > second ? "first is greater than second" :
first < second ? "first is less than second" : "first and second are equal";

No comments:

Post a Comment