1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Logicals and Comparison (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Logicals and Comparison (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Logicals and Comparison (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Execution-Control-Expressions.html" rel="up" title="Execution Control Expressions">
|
---|
33 | <link href="Logicals-and-Assignments.html" rel="next" title="Logicals and Assignments">
|
---|
34 | <link href="Logical-Operators.html" rel="prev" title="Logical Operators">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Logicals-and-Comparison"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Logicals-and-Assignments.html" accesskey="n" rel="next">Logicals and Assignments</a>, Previous: <a href="Logical-Operators.html" accesskey="p" rel="prev">Logical Operators</a>, Up: <a href="Execution-Control-Expressions.html" accesskey="u" rel="up">Execution Control Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Logical-Operators-and-Comparisons"></span><h3 class="section">8.2 Logical Operators and Comparisons</h3>
|
---|
64 |
|
---|
65 | <p>The most common thing to use inside the logical operators is a
|
---|
66 | comparison. Conveniently, ‘<samp>&&</samp>’ and ‘<samp>||</samp>’ have lower
|
---|
67 | precedence than comparison operators and arithmetic operators, so we
|
---|
68 | can write expressions like this without parentheses and get the
|
---|
69 | nesting that is natural: two comparison operations that must both be
|
---|
70 | true.
|
---|
71 | </p>
|
---|
72 | <div class="example">
|
---|
73 | <pre class="example">if (r != 0 && x % r == 0)
|
---|
74 | </pre></div>
|
---|
75 |
|
---|
76 | <p>This example also shows how it is useful that ‘<samp>&&</samp>’ guarantees to
|
---|
77 | skip the right operand if the left one turns out false. Because of
|
---|
78 | that, this code never tries to divide by zero.
|
---|
79 | </p>
|
---|
80 | <p>This is equivalent:
|
---|
81 | </p>
|
---|
82 | <div class="example">
|
---|
83 | <pre class="example">if (r && x % r == 0)
|
---|
84 | </pre></div>
|
---|
85 |
|
---|
86 | <p>A truth value is simply a number, so <code>r</code>
|
---|
87 | as a truth value tests whether it is nonzero.
|
---|
88 | But <code>r</code>’s meaning is not a truth value—it is a number to divide by.
|
---|
89 | So it is better style to write the explicit <code>!= 0</code>.
|
---|
90 | </p>
|
---|
91 | <p>Here’s another equivalent way to write it:
|
---|
92 | </p>
|
---|
93 | <div class="example">
|
---|
94 | <pre class="example">if (!(r == 0) && x % r == 0)
|
---|
95 | </pre></div>
|
---|
96 |
|
---|
97 | <p>This illustrates the unary ‘<samp>!</samp>’ operator, and the need to
|
---|
98 | write parentheses around its operand.
|
---|
99 | </p>
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | </body>
|
---|
104 | </html>
|
---|