C++ Ternary Basics clear

Last tutorial we have seen about ternary

But the question is why we use ternary ?

if(a>b){
c=10;
}else{
c=30;
}

the same we can write like this

(a>b)?(c=10):(c=30);

or like this

c=(a>b)?10:30;

So the syntax is:

(conditional expression)?expression1:expression2;

Just like the if else paradology

Code:

#include<iostream>
using namespace std;
int main()
{
int x,y=2;
x=(x<y)?10:40;
cout<<x<<endl;
return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.