The conditional expression
The purpose of the conditional expression is to select one of two expressions depending
on a third, boolean, expression. The format for the conditional expression is
<booleanexpression> ? <expression1> : <expression2>
If the value of the <booleanexpression> is true, the value of the conditional expression is
the value of <expression1>; if it is false, the value of the conditional expression is the
value of <expression2>.
An important point is that only one of <expression1> and <expression2> is evaluated
when evaluating the conditional expression. For example, the dividebyzero in the
following expression causes no error because it is not evaluated:
true ? 7 : 7/0
Expressions <expression1> and <expression2> may be any expressions, as long as the
have the same type (if one is wider than the other, the narrower one will automatically be
promoted to the wider type).
The conditional expression is known as a ternary
operation
, because it has three
operands.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 GRIES
 Algebra, Programming constructs

Click to edit the document details