* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5k.html
blob22b40db449fd69d3eb8b09b803e9cac6b7e4e073
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>section 2.11: Conditional Expressions</title>
10 <link href="sx5j.html" rev=precedes>
11 <link href="sx5l.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 2.11: Conditional Expressions</H2>
17 <p>``Ternary'' is a ten-dollar word meaning ``having three operands.''
18 (It's analogous to the terms unary and binary,
19 which refer to operators having one and two operands,
20 respectively.)
21 The conditional operator is a bit of a frill,
22 and it's a bit obscure,
24 so you may skip section 2.11 in the book on first reading,
25 but please read the comments in these notes just below
26 (under the mention of ``annoying compulsion'').
27 </p><p>page 52
28 </p><p>To see what the <TT>?:</TT> operator has bought us,
29 here is what the array-printing loop might look like without it:
30 <pre> for(i = 0; i &lt; n; i++) {
31 printf("%6d", a[i]);
32 if(i%10==9 || i==n-1)
33 printf("\n");
34 else printf(" ");
36 </pre></p><p>You may be finding this compulsion to write ``compact'' or
37 ``concise'' code using operators like <TT>++</TT> and <TT>+=</TT> and
38 <TT>?:</TT> a bit annoying.
39 There are three things to know:
40 <OL><li>In complicated code,
41 these operators allow an economy of expression
42 which <em>is</em> beneficial.
43 Mathematicians are constantly inventing new notations,
44 in which one letter or symbol
45 stands for a complicated expression or operation,
46 in order to solve complicated problems
47 without drowning in so much verbiage
48 that it would be impossible to follow an argument or check for errors.
49 Computer programs are large and complex,
50 so well-chosen abbreviations can make them easier to work with, too.
51 <li>Some C programmers, it's true,
52 do take the urge to write succinct or concise code to excess,
53 and end up with cryptic, bewildering, obfuscated, impenetrable messes.
54 (I'm not apologizing for them:
55 I hate
56 overly abbreviated,
57 impossible-to-read
58 code, too!)
59 <li>Since there is overly concise C code out there,
60 it's occasionally necessary to dissect a piece of it and figure
61 out what it does,
62 so you need to have enough familiarity with these operators,
63 and with some standard, idiomatic ways in which they're
64 commonly combined,
65 so that you won't be utterly stymied.
66 </OL>However,
67 there is <em>nothing</em> that says that you
68 have
69 to write concise code yourself.
70 Don't be lured into thinking
71 that you're not a ``real C programmer''
72 until you routinely and easily write code which no one else can read.
73 Write in a style that's comfortable to you;
74 don't be embarrassed if your code seems ``simple.''
75 (Actually, the very best code seems simple, too.)
76 With time,
77 you'll probably come to appreciate at least some of the idioms,
78 and to be comfortable enough with them
79 that you may want to use a few of them yourself,
80 after all.
81 </p><hr>
82 <p>
83 Read sequentially:
84 <a href="sx5j.html" rev=precedes>prev</a>
85 <a href="sx5l.html" rel=precedes>next</a>
86 <a href="sx5.html" rev=subdocument>up</a>
87 <a href="top.html">top</a>
88 </p>
89 <p>
90 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
91 // <a href="copyright.html">Copyright</a> 1995, 1996
92 // <a href="mailto:scs@eskimo.com">mail feedback</a>
93 </p>
94 </body>
95 </html>