Nakib
Nakib

Reputation: 19

Using multiple ternary operator in C++

Suppose, I want to do this using ternary operator:

if(i=='r') r=true;
else if(i=='b') b=true;
else if(i=='g') g=true;

Here, r,b,c are bool type variable declared:

bool r=false,g=false,b=false;

I want to replace if and else statements using ternary operator. How can I do this? I have tried in this way:

(i=='r'?r=true : i=='g'? g=true:i=='b'? b=true:false);

Is it okay? How is this statement compiled?

Upvotes: 0

Views: 161

Answers (1)

Ramy
Ramy

Reputation: 72

why not use this code?

r=i=='r';
b=i=='b';
g=i=='g';

Upvotes: 3

Related Questions