📄 ch03.htm
字号:
</td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>Not Equals </p> </td> <td colspan=1 align="left"> <p><tt>!=</tt></p> </td> <td colspan=1 align="left"> <p><tt>100 != 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"> <p><tt>50 != 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>false</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>Greater Than </p> </td> <td colspan=1 align="left"> <p><tt>></tt></p> </td> <td colspan=1 align="left"> <p><tt>100 > 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"> <p><tt>50 > 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>false</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>Greater Than </p> </td> <td colspan=1 align="left"> <p><tt>>=</tt></p> </td> <td colspan=1 align="left"> <p><tt>100 >= 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>or Equals </p> </td> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"> <p><tt>50 >= 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>Less Than </p> </td> <td colspan=1 align="left"> <p><tt><</tt></p> </td> <td colspan=1 align="left"> <p><tt>100 < 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>false</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"> <p><tt>50 < 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>false</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>Less Than </p> </td> <td colspan=1 align="left"> <p><tt><=</tt></p> </td> <td colspan=1 align="left"> <p><tt>100 <= 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>false</tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>or Equals </p> </td> <td colspan=1 align="left"><br> </td> <td colspan=1 align="left"> <p><tt>50 <= 50; </tt></p> </td> <td colspan=1 align="left"> <p><tt>true</tt></p> </td> </tr></table><blockquote> <hr> <p><strong>WARNING: </strong> Many novice C++ programmers confuse the assignment operator (<tt>=</tt>) with the equals operator (<tt>==</tt>). This can create a nasty bug in your program.</p> <hr></blockquote><h3> <a name="Heading7">Logical Operators</a></h3><p>The problem with this <tt>while</tt> loop is that it tests only whether <tt>howManyLetters</tt> is less than the constant <tt>minLetters</tt>; you also need to test to find out whether <tt>howManyLetters</tt> is greater than <tt>maxLetters</tt>. </p><p>You <i>can</i> test them separately:</p><pre><tt>while ( howManyLetters < minLetters )</tt><tt>{</tt><tt> //...</tt><tt>}</tt><tt>while ( howManyLetters > maxLetters )</tt><tt>{</tt><tt> //...</tt><tt>}</tt></pre><p>This will work, but the code within both <tt>while</tt> loops will be identical. In fact, what you are really trying to say is that you want to repeat this work while <tt>howManyLetters</tt> is less than <tt>minLetters</tt> or while <tt>howManyLetters</tt> is greater than <tt>maxLetters</tt>. C++ enables you to make exactly that test using the <i>logical</i> <i>OR</i> <i>operator</i> (<tt>||</tt>). You create the logical OR operator by pressing <b>Shift+\</b> twice. <a name="_Toc448989112"></a></p><h4>The Logical OR Operator</h4><p>In this case, you are asking for the <tt>while</tt> loop to continue as long as either condition is true, so you use logical OR:</p><pre><tt>while ( howManyLetters < minLetters || howManyLetters > maxLetters )</tt><tt>{</tt><tt> //...</tt><tt>}</tt></pre><p>This code says that the statement (between the braces) is executed if it is true that <tt>howManyLetters</tt> is less than <tt>minLetters</tt> or if it is true that <tt>howManyLetters</tt> is greater than <tt>maxLetters</tt> (or if both conditions are true). <a name="_Toc448989113"></a></p><h4>The Logical AND Operator</h4><p>At other times, you might want to continue only if both conditions are true, in which case you want to use <tt>while (<i>condition 1</i> and <i>condition 2</i>)</tt>. The <i>logical AND operator</i> (<tt>&&</tt>) handles this condition. A logical AND statement evaluates two expressions, and if both expressions are true, the logical AND statement is true as well. </p><p>For example, you can test the following:</p><pre><tt>while ( howManyLetters > minLetters && howManyLetters < maxLetters )</tt><tt>{</tt><tt> //...</tt><tt>}</tt></pre><p>this statement executes only if it is true that <tt>howManyLetters</tt> is greater than <tt>minLetters</tt> and if it is also true that <tt>howManyLetters</tt> is less than <tt>maxLetters</tt>.</p><blockquote> <hr> <p> <b>Logical operator OR</b>--<tt>||</tt> created by two vertical lines, by pressing <b>Shift+backslash</b> (\) twice.</p> <p> <b>Logical operator AND</b>--&& created by two ampersands, by pressing <b>Shift+7</b> twice. <a name="_Toc450553969"></a></p> <hr></blockquote><p>The <tt>if</tt> StatementAn <tt>if</tt> statement allows you to take action only if a condition is true (and to skip the action or do something else if the condition is false). You use <tt>if</tt> statements every day:</p><p><tt>If it is raining, I'll take my umbrella.</tt></p><p><tt>If I have time, I'll walk the dog.</tt></p><p>If I don't walk the dog, I'll be sorry.</p><p>The simplest form of an <tt>if</tt> statement is this:</p><pre><tt>if (<i>expression</i>)</tt><tt> statement;</tt></pre><p>The <tt><i>expression</i></tt> in the parentheses can be any expression at all, but it usually contains one of the relational expressions. If the expression has the value <tt>false</tt>, the statement is skipped. If it evaluates to <tt>true</tt>, the statement executes. Once again, the statement can certainly be a compound statement between braces, as you see here. <a name="_Toc448989114"></a><a name="_Toc441727828"></a></p><h4>The Logical NOT Operator</h4><p>A <i>logical NOT statement</i> (<tt>!</tt>) evaluates <tt>true</tt> if the expression that is being tested is false. This is confusing at first, but an example will help. I might start by saying, "If it is raining, I'll bring my umbrella":</p><pre><tt>if ( raining )</tt><tt> BringUmbrella();</tt></pre><p>How do I express that I'll only go for a walk if it is <i>not </i>raining?</p><pre><tt>if ( ! raining )</tt><tt> GoForWalk();</tt></pre><p>I can also reverse these:</p><pre><tt>if ( ! raining )</tt><tt> LeaveUmbrella;</tt><tt>if ( raining )</tt><tt> GoForWalk;</tt></pre><p>You get the idea. </p><p>Thus</p><pre><tt>if ( ! valid )</tt></pre><p>is true only if <tt>valid</tt> is false.</p><blockquote> <hr> <p> <b>Logical NOT</b>--Evalutes <tt>true</tt> when something is not true, and <tt>false</tt> when it <i>is</i> true. </p> <hr></blockquote><p>You can use this nifty construct to turn your logical OR statement into a logical AND statement without changing its meaning. For example</p><pre><tt>while ( howManyLetters < minLetters || howManyLetters > maxLetters )</tt></pre><p>is exactly the same thing as</p><pre><tt>while ( (! (howManyLetters > minLetters) ) && </tt><tt>(! (howManyLetters > maxLetters ) _) )</tt></pre><p>The logic of this is easy to understand if you use values. Assume that <tt>minLetters</tt> is <tt>2</tt>, <tt>maxLetters</tt> is <tt>10</tt>, and <tt>howManyLetters</tt> is <tt>0</tt>.</p><p>In that case, the <tt>while</tt> loop executes because the left part of the statement is true (0 is less than 2). In an OR statement, only one side must be true for the entire statement to return <tt>true</tt>.</p><p>Thus,</p><pre><tt>while ( howManyLetters < minLetters || howManyLetters > maxLetters )</tt></pre><p>becomes</p><pre><tt>while ( 0 < 2 || 0 > 10 ) // substitute the values</tt></pre><p>becomes</p><pre><tt>while ( true || false ) // evaluate the truth of each side</tt></pre><p>becomes</p><pre><tt>while ( true ) // if either is true, the statement is true</tt></pre><p>The second statement,</p><pre><tt>while ( (! (howManyLetters > minLetters) ) &&</tt><tt>(! (howManyLetters > maxLetters ) ) )</tt></pre><p>becomes</p><pre><tt>while ( (! (0 > 2) ) && (! (0 > 10 ) _) )</tt></pre><p>Now each side must be evaluated. The NOT symbol reverses the truth of what follows. It is as if this said, "While it is <i>not</i> true that zero is greater than 2 <i>and</i> it is <i>not</i> true that zero is greater than 10."</p><p>Thus you get</p><pre><tt>while ( (! (false) ) && (! (false ) _) )</tt></pre><p>When you apply NOT to <tt>false</tt>, you get <tt>true</tt>:</p><pre><tt>while ( (true) ) && (true) )</tt></pre><p>With an AND statement, both sides must be true; in this case they are, so the statement will execute. <a name="_Toc441727829"></a><a name="_Toc448989115"></a></p><h3> <a name="Heading8">Short Circuit Evaluation</a></h3><p>When the compiler is evaluating an AND statement such as</p><pre><tt>while ( (x == 5) && (y == 5) )</tt></pre><p>the compiler evaluates the truth of the first statement (<tt>x==5</tt>); if this fails (that is, if x is not equal to five), the compiler does not go on to evaluate the truth or falsity of the second statement (<tt>y == 5</tt>) because AND requires that both be true.</p><p>Similarly, if the compiler is evaluating an OR statement such as</p><pre><tt>while ( (x == 5) || (y == 5) )</tt></pre><p>if the first statement is <tt>true</tt> (<tt>x == 5</tt>), the compiler never evaluates the second statement (<tt>y == 5</tt>) because the truth of either is sufficient in an OR statement. <a name="_Toc441727830"></a><a name="_Toc448989116"></a></p><h3> <a name="Heading9">Relational Precedence</a></h3><p>Relational operators and logical operators, because they are C++ expressions, each return a value of <tt>true</tt> or <tt>false</tt>. Like all expressions, they have a precedence order (see Appendix B, "Operator Precedence") that determines which relations are evaluated first. This fact is important when determining the value of the statement</p><pre><tt>if ( x > 5 && y > 5 || z > 5)</tt></pre><p>It might be that the programmer wanted this expression to evaluate <tt>true</tt> if both x and y are greater than <tt>5</tt> or if z is greater than <tt>5</tt>. On the other hand, the programmer might have wanted this expression to evaluate <tt>true</tt> only if x is greater than <tt>5</tt>, and if it is also true that either y is greater than <tt>5</tt> or z is greater than <tt>5</tt>.</p><p>If x is <tt>3</tt> and y and z are both <tt>10</tt>, the first interpretation is true (z is greater than <tt>5</tt>, so ignore x and y), but the second is false (it isn't true that x is greater than <tt>5</tt>, and it therefore doesn't matter what is on the right side of the <tt>&&</tt> symbol because both sides must be true.)</p><p>Although precedence determines which relation is evaluated first, parentheses can both change the order and make the statement clearer:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -