C30. C Conditional Expressions

Conditional Expressions
C's if statement allows a program to perform one of two actions depending on the value of a condition. C also provides an operator that allows an expression to pro­duce one of two values depending on the value of a condition.
The conditional operator consists of two symbols (? and :), which must be used together in the following way:
exprl ? expr2 : expr3
exprl, exprl, and expr3 can be expressions of any type. The resulting expression is said to be a conditional expression. The conditional operator is unique among
C operators in that it requires three operands instead of one or two. For this reason, it is often referred to as a ternary operator.
The conditional expression exprl ? exprl : expr3 should be read “if exprl then exprl else expr3.
The expression is evaluated in stages: exprl is evaluated first; if its value isn't zero, then exprl is evaluated, and its value is the value of the entire conditional expression. If the value of exprl is zero, then the value of expr3 is the value of the conditional.

No comments: