When to use ternary operator?

It is possible to replace multiple lines of code with a single line of code using ternary operator. It makes your code more readable. However, don't over do it. For example, You can replace the following code


\begin{lstlisting}
if (expression1) {
result = 1;
} else if (expression2) {
re...
...
} else if (expression3) {
result = 3;
} else {
result = 0;
}
\end{lstlisting}

with


\begin{lstlisting}
result = (expression1) ? 1 : (expression2) ? 2 : (expression3) ? 3 : 0;
\end{lstlisting}