What is a ternary operator?

Asked 07-Dec-2019
Viewed 488 times

1 Answer


Often in a program there arrives where the triggering of a particular situation causes the execution of a particular set of statement and another set of execution for any other condition. This is the place where the use of a conditional statement arises. The ternary operator (?:) is nothing but just the compressed or the shorthand version of the conditional statements like if…. else.
The syntax is as follows:
Expression? (value if true): (value if false)
In this, if the expression evaluates to true then the first part is executed or else the second one. The expression is separated from the values with the question mark sign (?). The false and the true results are separated from each other by a colon sign (:).