📄 page_208.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_208</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_207.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_208</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_209.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 208</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">result聽=聽line17聽-聽line23;<br />if聽(result聽<0.0)<br />{<br />聽聽聽聽cout聽聽<聽Check聽box聽24A聽<<聽endl;<br />聽聽聽聽result聽=聽0.0;<br />}<br />line24聽=聽result;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">This code does exactly what the tax form says it should. It computes the result of subtracting line 23 from line 17. Then it looks to see if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> is less than zero. If it is, the fragment prints a message telling the user to check box 24A and then sets </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> to zero. Finally, the calculated result (or zero, if the result is less than zero) is stored into a variable named line24.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">What happens if we leave out the left and right braces in the code fragment above? Let's look at it:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">result聽=聽line17聽-聽line23;聽聽聽聽聽聽聽聽聽聽聽聽//聽Incorrect聽version<br />if聽(result聽<聽0.0)<br />聽聽聽聽cout聽<<聽Check聽box聽24A聽<<聽endl;<br />聽聽聽聽result聽=0.0;<br />line24聽=聽result;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Despite the way we have indented the code, the compiler takes the thenclause to be a single statementthe output statement. If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> is less than zero, the computer executes the output statement, then sets </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> to zero, and then stores </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">line24</font><font face="Times New Roman, Times, Serif" size="3">. So far, so good. But if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> is initially greater than or equal to zero, the computer skips the then-clause and proceeds to the statement following the If statementthe assignment statement that sets </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> to zero. The unhappy outcome is that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">result</font><font face="Times New Roman, Times, Serif" size="3"> ends up as zero no matter what its initial value was! The moral here is not to rely on indentation alone; you can't fool the compiler. If you want a compound statement for a then-or else-clause, you must include the left and right braces.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3"><i>A Common Mistake</i></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Earlier we warned against confusing the = operator and the == operator. Here is an example of a mistake that every C++ programmer is guaranteed to make at least once in his or her career:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">cin聽>>聽n;<br />if聽(n聽=聽3)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Wrong<br />聽聽聽聽cout聽<<聽n聽equals聽3;<br />else<br />聽聽聽聽cout聽<<聽n聽doesn't聽equal聽3;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_207.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_208</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_209.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -