⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 004.htm

📁 21天内快速掌握c++语言
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The programs shown in this book
	are written to demonstrate the particular issues being discussed. They are kept intentionally
	simple; there is no attempt to &quot;bulletproof&quot; the code to protect against
	user error. In professional-quality code, every possible user error is anticipated
	and handled gracefully.
<HR>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<CENTER>
<H3><A NAME="Heading40"></A><FONT COLOR="#000077">Logical Operators</FONT></H3>
</CENTER>
<P>Often you want to ask more than one relational question at a time. &quot;Is it
true that x is greater than y, and also true that y is greater than z?&quot; A program
might need to determine that both of these conditions are true, or that some other
condition is true, in order to take an action.</P>
<P>Imagine a sophisticated alarm system that has this logic: &quot;If the door alarm
sounds AND it is after six p.m. AND it is NOT a holiday, OR if it is a weekend, then
call the police.&quot; C++'s three logical operators are used to make this kind of
evaluation. These operators are listed in Table 4.2.<BR>
<BR>
<FONT SIZE="4"><B>Table 4.2. The Logical Operators. </B></FONT>
<TABLE BORDER="0">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><B><I>Operator</I></B></TD>
		<TD ALIGN="LEFT"><B><I>Symbol</I></B></TD>
		<TD ALIGN="LEFT"><B><I>Example</I></B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><TT>AND</TT></TD>
		<TD ALIGN="LEFT"><TT>&amp;&amp;</TT></TD>
		<TD ALIGN="LEFT">expression1<TT> &amp;&amp; </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>
<BR>
<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" font style="font-size:10pt">if ( (x == 5) &amp;&amp; (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>&amp;&amp;</TT> symbols. A single
<TT>&amp;</TT> symbol is a different operator, discussed on Day 21, &quot;What's
Next.&quot;
<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" font style="font-size:10pt">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" font style="font-size:10pt">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" font style="font-size:10pt">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" font style="font-size:10pt">if ( x &gt; 5 &amp;&amp;  y &gt; 5  || z &gt; 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" font style="font-size:10pt">if (  (x &gt; 5)  &amp;&amp; (y &gt; 5 ||  z &gt; 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 &quot;good tasting&quot; AND &quot;good
for you&quot; if it isn't good tasting.
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<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" font style="font-size:10pt">if (x)           // if x is true (nonzero)
x = 0;
</FONT></PRE>
<P>can be read as &quot;If <TT>x</TT> has a nonzero value, set it to 0.&quot; This
is a bit of a cheat; it would be clearer if written</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">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" font style="font-size:10pt">
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.
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<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" font style="font-size:10pt">(expression1) ? (expression2) : (expression3)
</FONT></PRE>
<P>This line is read as &quot;If expression1 is true, return the value of expression2;
otherwise, return the value of expression3.&quot; 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>
<BR>
<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" font style="font-size:10pt">1:   // Listing 4.9 - demonstrates the conditional operator
2:   //
3:   #include &lt;iostream.h&gt;
4:   int main()
5:   {
6:      int x, y, z;
7:      cout &lt;&lt; &quot;Enter two numbers.\n&quot;;
8:      cout &lt;&lt; &quot;First: &quot;;
9:      cin &gt;&gt; x;
10:     cout &lt;&lt; &quot;\nSecond: &quot;;
11:     cin &gt;&gt; y;
12:     cout &lt;&lt; &quot;\n&quot;;
13:
14:     if (x &gt; y)
15:       z = x;
16:     else
17:       z = y;
18:
19:     cout &lt;&lt; &quot;z: &quot; &lt;&lt; z;
20:     cout &lt;&lt; &quot;\n&quot;;
21:
22:     z =  (x &gt; y) ? x : y;
23:
24:     cout &lt;&lt; &quot;z: &quot; &lt;&lt; z;
25:     cout &lt;&lt; &quot;\n&quot;;
26:        return 0;
<TT>27: }</TT>
Output: Enter two numbers.
First: 5
<BR>
Second: 8
<BR>
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: &quot;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>.&quot;
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&amp;A</FONT></H3>
</CENTER>
<BR>
<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>
<BR>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">if ( (x = a + b) == 35 )
</FONT></PRE>
<BR>
<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</T

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -