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. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>3.3 Boolean Expressions
</title>
10 <link href=
"sx3b.html" rev=precedes
>
11 <link href=
"sx3d.html" rel=precedes
>
12 <link href=
"sx3.html" rev=subdocument
>
15 <H2>3.3 Boolean Expressions
</H2>
17 <p>An
<TT>if
</TT> statement like
22 is perhaps deceptively simple.
23 Conceptually, we say that it checks whether the condition
24 <TT>x
> max
</TT> is ``true'' or ``false''.
25 The mechanics underlying C's conception of ``true''
26 and ``false,'' however, deserve some explanation.
27 We need to understand how true and false values are represented,
28 and how they are interpreted by statements like
<TT>if
</TT>.
29 </p><p>As far as C is concerned,
30 a true/false condition can be represented as an integer.
31 (An integer can represent many values;
32 here we care about only two values:
33 ``true'' and ``false.''
34 The study of mathematics involving only two values is called
35 Boolean algebra, after George Boole, a mathematician who
37 In C, ``false'' is represented by a value of
0 (zero),
38 and ``true'' is represented by any value that is nonzero.
39 Since there are many nonzero values (at least
65,
534, for
40 values of type
<TT>int
</TT>),
41 when we have to pick a specific value for ``true,''
43 </p><p>The
<dfn>relational operators
</dfn> such as
44 <TT><</TT>,
<TT><=
</TT>,
<TT>></TT>, and
<TT>>=
</TT>
45 are in fact operators, just like
<TT>+
</TT>,
<TT>-
</TT>,
<TT>*
</TT>,
47 The relational operators take two values, look at them, and
48 ``return'' a value of
1 or
0 depending on whether the
49 tested relation was true or false.
50 The complete set of relational operators in C is:
54 <TT><</TT> less than
55 <TT><=
</TT> less than or equal
56 <TT>></TT> greater than
57 <TT>>=
</TT> greater than or equal
64 <TT>1 < 2</TT> is
1,
65 <TT>3 > 4</TT> is
0,
69 </p><p>We've now encountered perhaps the most easy-to-stumble-on
71 the equality-testing operator is
<TT>==
</TT>,
72 not a single
<TT>=
</TT>, which is assignment.
73 If you accidentally write
77 (and you probably will at some point;
80 it will
<em>not
</em> test whether
<TT>a
</TT> is zero,
81 as you probably intended.
82 Instead, it will
<em>assign
</em> 0 to
<TT>a
</TT>,
83 and then perform the ``true'' branch
84 of the
<TT>if
</TT> statement if
<TT>a
</TT> is
<em>non
</em>zero.
85 But
<TT>a
</TT> will have just been assigned the value
0,
86 so the ``true'' branch will never be taken!
87 (This could drive you crazy while debugging--you
88 wanted to do something if
<TT>a
</TT> was
0,
89 and after the test,
<TT>a
</TT> <em>is
</em> 0,
90 whether it was supposed to be or not,
91 but the ``true'' branch is nevertheless not taken.)
92 </p><p>The relational operators work with arbitrary numbers
93 and generate true/false values.
96 combine true/false values by using the
97 <dfn>Boolean operators
</dfn>,
98 which take true/false values as operands
99 and compute new true/false values.
100 The three Boolean operators are:
104 <TT>&&</TT> and
106 <TT>!
</TT> not (takes one operand; ``
<dfn>unary
</dfn>'')
110 The
<TT>&&</TT> (``and'') operator
111 takes two true/false values
112 and produces a true (
1) result if both operands are true
113 (that is, if the left-hand side is true
<B>and
</B>
114 the right-hand side is true).
115 The
<TT>||
</TT> (``or'') operator
116 takes two true/false values
117 and produces a true (
1) result if either operand is true.
118 The
<TT>!
</TT> (``not'') operator
119 takes a single true/false value and negates it,
120 turning false to true and true to false
121 (
0 to
1 and nonzero to
0).
122 </p><p>For example, to test whether the variable
<TT>i
</TT> lies
123 between
1 and
10, you might use
125 if(
1 < i
&& i
< 10)
128 Here we're expressing the
131 ``
<TT>i
</TT> is between
1 and
10''
133 ``
1 is less than
<TT>i
</TT> <B>and
</B>
134 <TT>i
</TT> is less than
10.''
135 </p><p>It's important to understand why the more obvious
139 if(
1 < i
< 10) /* WRONG */
143 <TT>1 < i
< 10</TT>
144 is parsed by the compiler analogously to
146 The expression
<TT>1 + i +
10</TT>
147 is parsed as
<TT>(
1 + i) +
10</TT>
149 means ``add
1 to
<TT>i
</TT>, and then add the result to
10.''
152 <TT>1 < i
< 10</TT>
153 is parsed as
<TT>(
1 < i)
< 10</TT>
155 means ``see if
1 is less than
<TT>i
</TT>,
156 and then see if the result is less than
10.''
157 But in this case, ``the result'' is
1 or
0, depending
158 on whether
<TT>i
</TT> is greater than
1.
159 Since both
0 and
1 are less than
10, the expression
160 <TT>1 < i
< 10</TT>
161 would
<em>always
</em> be true in C,
162 regardless of the value of
<TT>i
</TT>!
163 </p><p>Relational and Boolean expressions are usually used
164 in contexts such as an
<TT>if
</TT> statement, where something is
165 to be done or not done depending on some condition.
167 what's actually checked is whether the expression representing
168 the condition has a zero or nonzero value.
169 As long as the expression is a relational or Boolean expression,
170 the interpretation is just what we want.
176 the
<TT>></TT> operator produced a
1
177 if
<TT>x
</TT> was greater than
<TT>max
</TT>,
179 The
<TT>if
</TT> statement interprets
0 as false and
1
180 (or any nonzero value)
182 </p><p>But what if the expression is not a relational or Boolean expression?
183 As far as C is concerned,
184 the controlling expression
185 (of conditional statements like
<TT>if
</TT>)
188 be
<em>any
</em> expression:
189 it doesn't have to ``look like'' a Boolean expression;
190 it doesn't have to contain relational or logical operators.
192 (when it's evaluating an
<TT>if
</TT> statement,
193 or anywhere else where it needs a true/false value)
194 is whether the expression evaluates to
0 or nonzero.
197 a variable
<TT>x
</TT>,
198 and you want to do something if
<TT>x
</TT> is nonzero,
199 it's possible to write
204 and the statement will be executed if
<TT>x
</TT> is nonzero
205 (since nonzero means ``true'').
206 </p><p>This possibility
207 (that the controlling expression of an
<TT>if
</TT> statement
208 doesn't have to ``look like'' a Boolean expression)
209 is both useful and potentially confusing.
210 It's useful when you have a variable or a function that is
211 ``conceptually Boolean,''
214 that you consider to hold
215 a true or false (actually nonzero or zero) value.
216 For example, if you have a variable
<TT>verbose
</TT>
217 which contains a nonzero value when your program should run in
218 verbose mode and zero when it should be quiet,
219 you can write things like
222 printf(
"Starting first pass\n");
224 and this code is both legal and readable,
225 besides which it does what you want.
227 The standard library contains a function
<TT>isupper()
</TT>
228 which tests whether a character is an upper-case letter,
229 so if
<TT>c
</TT> is a character,
235 Both of these examples (
<TT>verbose
</TT> and
<TT>isupper()
</TT>)
236 are useful and readable.
237 </p><p>However, you will eventually come across code like
242 where
<TT>n
</TT> is just a number.
243 Here, the programmer wants to compute the average only if
244 <TT>n
</TT> is nonzero
245 (otherwise, of course, the code would divide by
0),
247 because, in the context of the
<TT>if
</TT> statement,
248 the trivial expression
<TT>n
</TT> is
250 interpreted as ``true'' if it is nonzero,
251 and ``false'' if it is zero.
252 </p><p>``Coding shortcuts''
256 but they're also quite common,
257 so you'll need to be able to recognize them
258 even if you don't choose to write them in your own code.
259 Whenever you see code like
267 where
<TT>x
</TT> or
<TT>f()
</TT>
268 do not have obvious ``Boolean'' names,
269 you can read them as ``if
<TT>x
</TT> is nonzero''
271 ``if
<TT>f()
</TT> returns nonzero.''
275 <a href=
"sx3b.html" rev=precedes
>prev
</a>
276 <a href=
"sx3d.html" rel=precedes
>next
</a>
277 <a href=
"sx3.html" rev=subdocument
>up
</a>
278 <a href=
"top.html">top
</a>
281 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
282 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
283 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>