## #92 – The Ternary Conditional Operator

September 17, 2010 Leave a comment

The conditional operator (**?:**) take three operands. The first is a boolean expression to evaluate. If this expression is true, the value of the conditional operator is equal to the second operand. If it’s false, the value of the conditional operator is equal to the third operand.

So for the expression

a ? b : c

the value of this expression is **b** if **a** is true, or **c** if **a** is false.

Here’s an example of using the conditional operator (assume that **isRaining** is a boolean):

string describeWeather = isRaining ? "Wet" : "Nice";

A conditional operator can always be rewritten as an **if** statement. The previous example is equivalent to:

string describeWeather; if (isRaining) describeWeather = "Wet"; else describeWeather = "Nice";

Using the conditional operator can lead to more concise code.