📄 ch04.htm
字号:
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>AND</TT></TD>
<TD ALIGN="LEFT"><TT>&&</TT></TD>
<TD ALIGN="LEFT">expression1<TT> && </TT>expression2</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>OR</TT></TD>
<TD ALIGN="LEFT"><TT>||</TT></TD>
<TD ALIGN="LEFT">expression1<TT> || </TT>expression2</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>NOT</TT></TD>
<TD ALIGN="LEFT"><TT>!</TT></TD>
<TD ALIGN="LEFT"><TT>!</TT>expression</TD>
</TR>
</TABLE>
<CENTER>
<H4><A NAME="Heading41"></A><FONT COLOR="#000077">Logical AND</FONT></H4>
</CENTER>
<P>A logical <TT>AND</TT> statement evaluates two expressions, and if both expressions
are true, the logical <TT>AND</TT> statement is true as well. If it is true that
you are hungry, AND it is true that you have money, THEN it is true that you can
buy lunch. Thus,</P>
<PRE><FONT COLOR="#0066FF">if ( (x == 5) && (y == 5) )
</FONT></PRE>
<P>would evaluate <TT>TRUE</TT> if both <TT>x</TT> and <TT>y</TT> are equal to 5,
and it would evaluate <TT>FALSE</TT> if either one is not equal to 5. Note that both
sides must be true for the entire expression to be true.</P>
<P>Note that the logical <TT>AND</TT> is two <TT>&&</TT> symbols. A single
<TT>&</TT> symbol is a different operator, discussed on Day 21, "What's
Next."
<CENTER>
<H4><A NAME="Heading42"></A><FONT COLOR="#000077">Logical OR</FONT></H4>
</CENTER>
<P>A logical <TT>OR</TT> statement evaluates two expressions. If either one is true,
the expression is true. If you have money OR you have a credit card, you can pay
the bill. You don't need both money and a credit card; you need only one, although
having both would be fine as well. Thus,</P>
<PRE><FONT COLOR="#0066FF">if ( (x == 5) || (y == 5) )
</FONT></PRE>
<P>evaluates <TT>TRUE</TT> if either <TT>x</TT> or <TT>y</TT> is equal to 5, or if
both are.</P>
<P>Note that the logical <TT>OR</TT> is two <TT>||</TT> symbols. A single <TT>|</TT>
symbol is a different operator, discussed on Day 21.
<CENTER>
<H4><A NAME="Heading43"></A><FONT COLOR="#000077">Logical NOT</FONT></H4>
</CENTER>
<P>A logical <TT>NOT</TT> statement evaluates <TT>true</TT> if the expression being
tested is false. Again, if the expression being tested is false, the value of the
test is <TT>TRUE</TT>! Thus</P>
<PRE><FONT COLOR="#0066FF">if ( !(x == 5) )
</FONT></PRE>
<P>is true only if <TT>x</TT> is not equal to 5. This is exactly the same as writing</P>
<PRE><FONT COLOR="#0066FF">if (x != 5)
</FONT></PRE>
<CENTER>
<H3><A NAME="Heading44"></A><FONT COLOR="#000077">Relational Precedence</FONT></H3>
</CENTER>
<P>Relational operators and logical operators, being C++ expressions, each return
a value: <TT>1</TT> (<TT>TRUE</TT>) or <TT>0</TT> (<TT>FALSE</TT>). Like all expressions,
they have a precedence order (see Appendix A) that determines which relations are
evaluated first. This fact is important when determining the value of the statement</P>
<PRE><FONT COLOR="#0066FF">if ( x > 5 && y > 5 || z > 5)
</FONT></PRE>
<P>It might be that the programmer wanted this expression to evaluate <TT>TRUE</TT>
if both <TT>x</TT> and <TT>y</TT> are greater than 5 or if <TT>z</TT> is greater
than 5. On the other hand, the programmer might have wanted this expression to evaluate
<TT>TRUE</TT> only if <TT>x</TT> is greater than 5 and if it is also true that either
<TT>y</TT> is greater than 5 or <TT>z</TT> is greater than 5.</P>
<P>If <TT>x</TT> is 3, and <TT>y</TT> and <TT>z</TT> are both 10, the first interpretation
will be true (<TT>z</TT> is greater than 5, so ignore <TT>x</TT> and <TT>y</TT>),
but the second will be false (it isn't true that both <TT>x</TT> and <TT>y</TT> are
greater than 5 nor is it true that <TT>z</TT> is greater than 5).</P>
<P>Although precedence will determine which relation is evaluated first, parentheses
can both change the order and make the statement clearer:</P>
<PRE><FONT COLOR="#0066FF">if ( (x > 5) && (y > 5 || z > 5) )
</FONT></PRE>
<P>Using the values from earlier, this statement is false. Because it is not true
that <TT>x</TT> is greater than 5, the left side of the <TT>AND</TT> statement fails,
and thus the entire statement is false. Remember that an <TT>AND</TT> statement requires
that both sides be true--something isn't both "good tasting" AND "good
for you" if it isn't good tasting.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>It is often a good idea to use extra
parentheses to clarify what you want to group. Remember, the goal is to write programs
that work and that are easy to read and understand.
<HR>
</BLOCKQUOTE>
<CENTER>
<H3><A NAME="Heading45"></A><FONT COLOR="#000077">More About Truth and Falsehood</FONT></H3>
</CENTER>
<P>In C++, zero is false, and any other value is true. Because an expression always
has a value, many C++ programmers take advantage of this feature in their <TT>if</TT>
statements. A statement such as</P>
<PRE><FONT COLOR="#0066FF">if (x) // if x is true (nonzero)
x = 0;
</FONT></PRE>
<P>can be read as "If <TT>x</TT> has a nonzero value, set it to 0." This
is a bit of a cheat; it would be clearer if written</P>
<PRE><FONT COLOR="#0066FF">if (x != 0) // if x is nonzero
x = 0;
</FONT></PRE>
<P>Both statements are legal, but the latter is clearer. It is good programming practice
to reserve the former method for true tests of logic, rather than for testing for
nonzero values.</P>
<P>These two statements also are equivalent:</P>
<PRE><FONT COLOR="#0066FF">
if (!x) // if x is false (zero)
if (x == 0) // if x is zero
</FONT></PRE>
<P>The second statement, however, is somewhat easier to understand and is more explicit.
<BLOCKQUOTE>
<P>
<HR>
<B>DO</B> put parentheses around your logical tests to make them clearer and to make
the precedence explicit. <B>DO</B> use braces in nested <TT>if</TT> statements to
make the <TT>else</TT> statements clearer and to avoid bugs. <B>DON'T</B> use <TT>if(x)</TT>
as a synonym for <TT>if(x != 0)</TT>; the latter is clearer. <B>DON'T</B> use <TT>if(!x)</TT>
as a synonym for <TT>if(x == 0)</TT>; the latter is clearer.
<HR>
</P>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>It is common to define your own
enumerated Boolean (logical) type with <TT>enum Bool {FALSE, TRUE};</TT>. This serves
to set <TT>FALSE</TT> to <TT>0</TT> and <TT>TRUE</TT> to <TT>1</TT>.
<HR>
</BLOCKQUOTE>
<CENTER>
<H3><A NAME="Heading46"></A><FONT COLOR="#000077">Conditional (Ternary) Operator</FONT></H3>
</CENTER>
<P>The conditional operator (<TT>?:</TT>) is C++'s only ternary operator; that is,
it is the only operator to take three terms.</P>
<P>The conditional operator takes three expressions and returns a value:</P>
<PRE><FONT COLOR="#0066FF">(expression1) ? (expression2) : (expression3)
</FONT></PRE>
<P>This line is read as "If expression1 is true, return the value of expression2;
otherwise, return the value of expression3." Typically, this value would be
assigned to a variable.</P>
<P>Listing 4.9 shows an <TT>if</TT> statement rewritten using the conditional operator.</P>
<P><A NAME="Heading47"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.9. A demonstration
of the conditional operator</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 4.9 - demonstrates the conditional operator
2: //
3: #include <iostream.h>
4: int main()
5: {
6: int x, y, z;
7: cout << "Enter two numbers.\n";
8: cout << "First: ";
9: cin >> x;
10: cout << "\nSecond: ";
11: cin >> y;
12: cout << "\n";
13:
14: if (x > y)
15: z = x;
16: else
17: z = y;
18:
19: cout << "z: " << z;
20: cout << "\n";
21:
22: z = (x > y) ? x : y;
23:
24: cout << "z: " << z;
25: cout << "\n";
26: return 0;
<TT>27: }</TT>
Output: Enter two numbers.
First: 5
Second: 8
z: 8
z: 8
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Three integer variables are created:
<TT>x</TT>, <TT>y</TT>, and <TT>z</TT>. The first two are given values by the user.
The <TT>if</TT> statement on line 14 tests to see which is larger and assigns the
larger value to <TT>z</TT>. This value is printed on line 19.<BR>
The conditional operator on line 22 makes the same test and assigns <TT>z</TT> the
larger value. It is read like this: "If <TT>x</TT> is greater than <TT>y</TT>,
return the value of <TT>x</TT>; otherwise, return the value of <TT>y</TT>."
The value returned is assigned to <TT>z</TT>. That value is printed on line 24. As
you can see, the conditional statement is a shorter equivalent to the <TT>if...else</TT>
statement.
<CENTER>
<H3><A NAME="Heading49"></A><FONT COLOR="#000077">Summary</FONT></H3>
</CENTER>
<P>This chapter has covered a lot of material. You have learned what C++ statements
and expressions are, what C++ operators do, and how C++ <TT>if</TT> statements work.</P>
<P>You have seen that a block of statements enclosed by a pair of braces can be used
anywhere a single statement can be used.</P>
<P>You have learned that every expression evaluates to a value, and that value can
be tested in an <TT>if</TT> statement or by using the conditional operator. You've
also seen how to evaluate multiple statements using the logical operator, how to
compare values using the relational operators, and how to assign values using the
assignment operator.</P>
<P>You have explored operator precedence. And you have seen how parentheses can be
used to change the precedence and to make precedence explicit and thus easier to
manage.
<CENTER>
<H3><A NAME="Heading50"></A><FONT COLOR="#000077">Q&A</FONT></H3>
</CENTER>
<DL>
<DD><B>Q. Why use unnecessary parentheses when precedence will determine which operators
are acted on first?<BR>
</B><BR>
<B>A.</B> Although it is true that the compiler will know the precedence and that
a programmer can look up the precedence order, code that is easy to understand is
easier to maintain.<BR>
<BR>
<B>Q. If the relational operators always return 1 or 0, why are other values considered
true?<BR>
</B><BR>
<B>A. </B>The relational operators return 1 or 0, but every expression returns a
value, and those values can also be evaluated in an <TT>if</TT> statement. Here's
an example:
</DL>
<PRE><FONT COLOR="#0066FF">if ( (x = a + b) == 35 )
</FONT></PRE>
<DL>
<DD>This is a perfectly legal C++ statement. It evaluates to a value even if the
sum of <TT>a</TT> and <TT>b</TT> is not equal to 35. Also note that <TT>x</TT> is
assigned the value that is the sum of <TT>a</TT> and <TT>b</TT> in any case.<BR>
<BR>
<B>Q. What effect do tabs, spaces, and new lines have on the program?<BR>
</B><BR>
A. Tabs, spaces, and new lines (known as whitespace) have no effect on the program,
although judicious use of whitespace can make the program easier to read.<BR>
<BR>
<B>Q. Are negative numbers true or false?<BR>
</B><BR>
<B>A.</B> All nonzero numbers, positive and negative, are true.
</DL>
<CENTER>
<H3><A NAME="Heading51"></A><FONT COLOR="#000077">Workshop</FONT></H3>
</CENTER>
<P>The Workshop provides quiz questions to help you solidify your understanding of
the material covered, and exercises to provide you with experience in using what
you've learned. Try to answer the quiz and exercise questions before checking the
answers in Appendix D, and make sure that you understand the answers before continuing
to the next chapter.
<CENTER>
<H4><A NAME="Heading52"></A><FONT COLOR="#000077">Quiz</FONT></H4>
</CENTER>
<DL>
<DD><B>1.</B> What is an expression?<BR>
<B><BR>
2.</B> Is <TT>x = 5 + 7</TT> an expression? What is its value?<BR>
<B><BR>
3.</B> What is the value of <TT>201 / 4</TT>?<BR>
<B><BR>
4.</B> What is the value of <TT>201 % 4</TT>?<BR>
<B><BR>
5.</B> If <TT>myAge</TT>, <TT>a</TT>, and <TT>b</TT> are all <TT>int</TT> variables,
what are their values after:
</DL>
<PRE><FONT COLOR="#0066FF">myAge = 39;
a = myAge++
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -