📄 page_477.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_477</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_476.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_477</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_478.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 477</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="Times New Roman, Times, Serif" size="3">To avoid duplicating the invariant this way, we include only one copy at the top of the loop body, along with the remark prior to test. This remark is a reminder that the invariant is logically, if not physically, located just before the loop test. Here is a While loop to sum the integers 1 through 10:</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">sum聽=聽0;<br />count聽=聽1;<br />while聽(count聽<=聽10)<br />{<br /><br />聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽//聽聽聽聽聽sum聽==聽1聽+聽2聽+聽聽+聽count-1<br />聽聽聽聽聽聽聽聽//聽聽&&聽1聽<=聽count聽<=聽11<br /><br />聽聽聽聽sum聽=聽sum聽+聽count;<br />聽聽聽聽count++;<br /><br />}</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">In comparing this example to the Do-While version, there are two things to notice. First, the two versions use different invariants for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">count</font><font face="Times New Roman, Times, Serif" size="3">. In the Do-While loop, the lower limit is 2 because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">count</font><font face="Times New Roman, Times, Serif" size="3"> has already been incremented by the time the invariant is reached. In the While loop version, the invariant is first encountered <i>before</i> the loop begins. At that moment, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">count</font><font face="Times New Roman, Times, Serif" size="3"> equals 1.</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">The second thing to notice is that the invariant for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sum</font><font face="Times New Roman, Times, Serif" size="3"> seems to be wrong the first time the invariant is encountered. Before the loop begins, the assertion is</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">sum聽=聽1聽+聽2聽+聽聽+聽0</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">Is this a true assertion? The answer is yes, and here's why. The assertion, if written out in full, is actually this: For all integers in the range 1 through 0 in increasing order, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sum</font><font face="Times New Roman, Times, Serif" size="3"> is the sum of these integers. Because there aren't any integers that satisfy the For all part, the assertion is trivially true. Here's another example of a loop invariant that is trivially true before the first execution of the loop body:</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">for聽(i聽=聽1;聽i聽<=聽50;聽i++)<br /><br />聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test)聽:<br />聽聽聽聽聽聽聽聽//聽聽聽聽聽The聽values聽1,聽2,聽,聽il聽have聽been聽output<br />聽聽聽聽聽聽聽聽//聽聽&&聽1聽<=聽i聽<=聽51<br /><br />聽聽聽聽cout聽<<聽i聽<<聽endl;</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">After </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">i</font><font face="Times New Roman, Times, Serif" size="3"> has been initialized to 1 but before the loop test occurs, the assertion</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">The values 1,2, , 0 have been output</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_476.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_477</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_478.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -