📄 page_265.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_265</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_264.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_265</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_266.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 265</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">聽聽聽聽if聽(number聽<聽0)聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Test聽input聽value<br />聽聽聽聽聽聽聽聽nonNegative聽=聽FALSE;聽聽聽聽聽//聽Set聽flag聽if聽event聽occurred<br />聽聽聽聽else<br />聽聽聽聽聽聽聽聽sum聽=聽sum聽+聽number;<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">Notice that we can code sentinel-controlled loops with flags. In fact, this code uses a negative value as a sentinel.</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">You do not have to initialize flags to TRUE; you can initialize them to FALSE. If you do, you must use the NOT operator (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">!</font><font face="Times New Roman, Times, Serif" size="3">) in the While expression and reset the flag to TRUE when the event occurs. Compare the code segment above with the one below; both perform the same task. (Assume that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">negative</font><font face="Times New Roman, Times, Serif" size="3"> is a Boolean variable.)</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 />negative聽=聽FALSE;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Initialize聽flag<br />while聽(聽!negative聽)<br />{<br />聽聽聽聽cin聽>>聽number;<br />聽聽聽聽if聽(number聽<聽0)聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Test聽input聽value<br />聽聽聽聽聽聽聽聽negative聽=聽TRUE;聽聽聽聽聽聽聽聽聽//聽Set聽flag聽if聽event聽occurred<br />聽聽聽聽else<br />聽聽聽聽聽聽聽聽sum聽=聽sum聽+聽number;<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">As one more example, look at the While statement in the Payroll program of Chapter 1 (page 35). This is a sentinel-controlled loop because an employee number (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">empNum</font><font face="Times New Roman, Times, Serif" size="3">) with a value of 0 is used to stop the loop. We could have used a flag instead, as follows. (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">moreData</font><font face="Times New Roman, Times, Serif" size="3"> is a Boolean variable.)</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聽>>聽empNum;<br />moreData聽=聽(empNum聽!=聽0);聽聽聽聽聽聽聽聽//聽moreData聽is聽TRUE聽if聽empNum聽!=聽0<br />while聽(moreData)<br />{<br />聽聽聽聽.<br />聽聽聽聽.<br />聽聽聽聽.<br />聽聽聽聽cin聽>>聽empNum;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Get聽the聽next聽employee聽number<br />聽聽聽聽moreData聽=聽(empNum聽!=聽0);聽聽聽聽//聽Update聽the聽flag聽accordingly<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"><i>Looping Subtasks</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">We have been looking at ways to use loops to affect the flow of control in programs. Looping by itself does nothing. The loop body must perform a task in order for the loop to accomplish something. In this section, we look at three taskscounting, summing, and keeping track of a previous valuethat often are used in loops.</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_264.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_265</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_266.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -