The Conditional Operator

The Conditional Operator

The conditional operator is used as an alternative of a simple if-else statement.
The conditional operator consists of "?" (question mark) and a ":" (colon) . Its syntax is 

{Condition} ? {Exp 1} : {Exp 2}

Where

Condition                      represents the test condition. If this condition is true then the value of "exp 1"                                        is returned after evaluation. Otherwise the value of "exp 2" is returned.

exp1 & exp2                  represents the two expressions. It may be arithmetic expressions or constant                                          values. Input/Output statements can be used.

For example, if a=10 and b=5 and res  is an integer type variable, then an assignment statement is written as:

res = ( a> b) ? a:b

The above statement is equivalent to:
 if ( a>b)
res =a;
else
res=b;
Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.