* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5f.html
bloba6c35ce065c16889def3d1ca1e44ef8e70827031
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.6: Relational and Logical Operators</title>
10 <link href="sx5e.html" rev=precedes>
11 <link href="sx5g.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 2.6: Relational and Logical Operators</H2>
17 <p>If it isn't obvious, <TT>&gt;=</TT> is greater-than-or-equal-to,
18 <TT>&lt;=</TT> is less-than-or-equal-to,
19 <TT>==</TT> is equal-to,
20 and
21 <TT>!=</TT> is not-equal-to.
22 We use <TT>&gt;=</TT>, <TT>&lt;=</TT>, and <TT>!=</TT> because the
23 symbols >=, <=, and != are not common on computer keyboards,
24 and we use <TT>==</TT> because equality testing and assignment
25 are two completely different operations, but <TT>=</TT> is
26 already taken for assignment.
27 (Obviously, typing <TT>=</TT> when you mean <TT>==</TT> is a very
28 easy mistake to make, so watch for it.
29 Some compilers will warn you when you use one but seem to want
30 the other.)
31 </p><p>The fact that evaluation of the logical operators
32 <TT>&amp;&amp;</TT> and <TT>||</TT>
33 ``stops as soon as the truth or falsehood of the result is known''
34 refers to the fact that
35 <blockquote>``false'' AND anything is false
36 </blockquote>or, in C,
37 <blockquote><TT>(0 &amp;&amp; </TT><I>anything</I><TT>) == 0</TT>
38 </blockquote>while, on the other hand,
39 <blockquote>``true'' OR anything is true
40 </blockquote>or, in C,
41 <blockquote><TT>(1 || </TT><I>anything</I><TT>) == 1</TT>
42 </blockquote>Looking at these another way,
43 if you want to do something if thing1 is true and thing2 is true,
44 and you've just noticed that thing1 is false,
45 you don't even need to check thing2.
46 Similarly,
47 if you're supposed to do something if thing3 is true or thing4 is true,
48 and you notice that thing3 is true,
49 you can go ahead and do whatever it is you're supposed to do
50 without checking thing4.
51 </p><p>C works the same way, and if it's not true that
52 ``most C programs rely on these properties,''
53 it's certainly true that many do.
54 </p><p>For another example of the usefulness of this ``short-circuiting'' behavior,
55 suppose we're taking the average of <TT>n</TT> numbers.
56 If <TT>n</TT> is zero, that is,
57 if we don't have any numbers to take the average of,
58 we don't want to divide by zero.
59 Code like
60 <pre> if(n != 0 &amp;&amp; sum / n &gt; 1)
61 </pre>is common:
62 it tests whether <TT>n</TT> is nonzero and the average is greater than 1,
63 but it does not have to worry about dividing by zero.
64 (If,
65 on the other hand,
66 the compiler always evaluated both sides of the <TT>&amp;&amp;</TT>
67 before checking to see whether they were both true,
68 the code above <em>could</em> divide by zero.)
69 </p><p>page 42
70 </p><p>Note the extra parentheses in
71 <pre> (c = getchar()) != '\n'
72 </pre>Since this is a common idiom,
73 you'll need to remember the parentheses.
74 What would
75 <pre> c = getchar() != '\n'
76 </pre>do?
77 </p><p>C's treatment of Boolean values
78 (that is, those where we only care whether they're true or false)
79 is straightforward.
80 We'll have more to say about it later,
81 but for now,
82 note that a value of zero is ``false,''
83 and any nonzero value is ``true.''
84 You might
85 also note that there is no necessary connection
86 between statements like <TT>if()</TT> which expect a true/false value
87 and operators like <TT>&gt;=</TT> and <TT>&amp;&amp;</TT>
88 which generate true/false values.
89 You can use operators like <TT>&gt;=</TT> and <TT>&amp;&amp;</TT>
90 in any expression,
91 and you can use any expression in an <TT>if()</TT> statement.
92 </p><p>The authors make a good point about style:
93 if <TT>valid</TT> is conceptually a Boolean variable
94 (that is, it's an integer,
95 but we only care about whether it's zero or nonzero,
96 in other words,
97 ``false'' or ``true''),
98 then
99 <pre> if(valid)
100 </pre>is a perfectly reasonable and readable condition.
101 However, when values are not conceptually Boolean,
102 I encourage you to make explicit comparisons against 0.
103 For example, we could have expressed our average-taking code as
104 <pre> if(n &amp;&amp; sum / n &gt; 1)
105 </pre>but I think it's clearer to be explicit and say
106 <pre> if(n != 0 &amp;&amp; sum / n &gt; 1)
107 </pre>(However, many C programmers feel that expressions like
108 <pre> if(n &amp;&amp; sum / n &gt; 1)
109 </pre>are ``more concise,'' so you will see them all the time
110 and you should be able to read them.)
111 </p><hr>
113 Read sequentially:
114 <a href="sx5e.html" rev=precedes>prev</a>
115 <a href="sx5g.html" rel=precedes>next</a>
116 <a href="sx5.html" rev=subdocument>up</a>
117 <a href="top.html">top</a>
118 </p>
120 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
121 // <a href="copyright.html">Copyright</a> 1995, 1996
122 // <a href="mailto:scs@eskimo.com">mail feedback</a>
123 </p>
124 </body>
125 </html>