Thursday, November 27, 2025

Keep code clean

Take a look at this follow if else conditional statement.

-- conditional statement
if (a==0)
    ans = 1
else
    ans = 2;

-- ternary operator
ans = a == 0 ? 1 : 2 ;

Ternary Operator is a compact way of writing conditional expression and also achieve the same result.
It is used for shortening the if else statement and makes the code cleaner.