171k views
5 votes
Ternary operators of computer

please explain. ​

User Vikas J
by
6.8k points

1 Answer

7 votes

Answer:

It's a compact way of doing an if-else statement.

General Format is

<condition> ? <if condition is true> : <else>;

Example:

I could rewrite:

if(a==1) temp = 1;

else temp = 999;

as

temp = (a==1) ? 1 : 999;

User Sayooj
by
7.1k points