From the course: C Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using the ternary operator

Using the ternary operator - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Using the ternary operator

- [Instructor] In this exercise file, you see a standard if-else construction. If a is greater than b, assign its value to variable c, otherwise, assign the value of variable b to c, output the results, build and run. And there you see the output. Here is the same code, but with the if-else construction replaced by a ternary operator statement at line nine. Like the if keyword, the ternary operator involves a comparison. It comes first. Is a greater than b? This expression, the comparison, need not be enclosed in parentheses. I did it here to make this new format look a little bit more readable. The expression is followed by a question mark, which is the first piece of the ternary operator. If this comparison is true, then the result is the value a, whatever comes after the question mark. This value is assigned to variable c. If this expression is false, then what follows the colon is assigned to a variable c.…

Contents