* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx4eb.html
blob062d9369b69253943c14fd01ab57e848608262dc
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>18.2.5: The Conditional Operator</title>
10 <link href="sx4db.html" rev=precedes>
11 <link href="sx4c.html" rel=precedes>
12 <link href="sx4b.html" rev=subdocument>
13 </head>
14 <body>
15 <H3>18.2.5: The Conditional Operator</H3>
17 <p>[This section corresponds to K&amp;R Sec. 2.11]
18 </p><p>C has one last operator which we haven't seen yet.
19 It's called the
20 conditional or ``ternary'' or <TT>?:</TT> operator,
21 and in action it looks something like this:
22 <pre>
23 average = (n &gt; 0) ? sum / n : 0
24 </pre>
25 </p><p>The syntax of the conditional operator is
26 <pre>
27 <I>e1</I> ? <I>e2</I> : <I>e3</I>
28 </pre>
29 and what happens is that <I>e1</I> is evaluated,
30 and if it's true then <I>e2</I> is evaluated
31 and becomes the result of the expression,
32 otherwise
33 <I>e3</I> is evaluated and becomes the result of the expression.
34 In other words,
35 the conditional expression is
36 sort of an <TT>if</TT>/<TT>else</TT> statement
37 buried inside of an expression.
38 The above computation of <TT>average</TT> could be written out
39 in a longer form using an <TT>if</TT> statement:
40 <pre>
41 if(n &gt; 0)
42 average = sum / n;
43 else average = 0;
44 </pre>
45 </p><p>The conditional operator,
46 however,
47 forms an expression
48 and can therefore be used wherever an expression can be used.
49 This makes it more convenient to use
50 when an <TT>if</TT> statement
51 would otherwise cause other
52 sections
53 of code to be needlessly repeated.
54 For example,
55 suppose we were trying to write a complicated function call
56 <pre>
57 func(a, b + 1, c + d, <I>xx</I>, (g + h + i) / 2);
58 </pre>
59 where <I>xx</I> was supposed to be <TT>f</TT> if <TT>e</TT> was true
60 and 0 if it was not.
61 Using an <TT>if</TT> statement, we'd have to write:
62 <pre>
63 if(e)
64 func(a, b + 1, c + d, f, (g + h + i) / 2);
65 else func(a, b + 1, c + d, 0, (g + h + i) / 2);
66 </pre>
67 We could write this more compactly, more readably, and more safely
68 (it's easier both to see and to guarantee
69 that the other arguments are always the same)
70 by writing
71 <pre>
72 func(a, b + 1, c + d, e ? f : 0, (g + h + i) / 2);
73 </pre>
74 </p><p>(The obscure name ``ternary,'' by the way,
75 comes from the fact that the conditional operator
76 is neither unary nor binary;
77 it takes <em>three</em> operands.)
78 </p><hr>
79 <p>
80 Read sequentially:
81 <a href="sx4db.html" rev=precedes>prev</a>
82 <a href="sx4c.html" rel=precedes>next</a>
83 <a href="sx4b.html" rev=subdocument>up</a>
84 <a href="top.html">top</a>
85 </p>
86 <p>
87 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
88 // <a href="copyright.html">Copyright</a> 1996-1999
89 // <a href="mailto:scs@eskimo.com">mail feedback</a>
90 </p>
91 </body>
92 </html>