Computer Science, asked by nidhi0306, 5 months ago

Write syntax of ternary operator and give an example.
Please help me​

Answers

Answered by Anonymous
0

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c)

Answered by Oreki
1

\text{\bf\large Ternary Operator}

   \textsf{It is a short-cut notation of simple conditional statements.}

     \textsf{\textbf{Syntax}}\\\texttt{\hspace{1em} &lt;test-condition&gt; ? &lt;statement1&gt; : &lt;statement2&gt;; }

          \textsf{When the condition is evaluated to true the statement after the \texttt{?} is executed}\\\textsf{otherwise the statement after the \texttt{:} gets executed.}

     \textsf{\textbf{Example}}\\\texttt{\hspace{1em} int a = 50, b = 5;}\\\texttt{\hspace{1em} int c = (a &gt; b) ? a : b;}\\\texttt{\hspace{1em} \symbol{92}\symbol{92} Thus, 50 is stored in c as a is more than b }\\

Similar questions