Course Content
Programming in C
About Lesson

Conditional expressions use ternary operators: ‘?’ and ‘:’ to provide an alternate way to write if else statements. It’s takes the form:

expr1 ? expr2 : expr3

expr1 is first evaluated, if it’s true then expr2 is evaluated and that becomes the value of the conditional expression, else expr3 is evaluated and that is the value.
To find the minimum of a and b, we could use if else like this:

if (a < b)
  min = a;
else
  min = b;

or use the ternary operators:

min = (a < b) ? a : b;
Scroll to Top