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

📄 page_472.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_472</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_471.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_472</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_473.html">next page&nbsp;&gt;</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 472</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td>  <td colspan="3" height="17"></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 Break and Continue Statements</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 </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> statement, which we introduced with the Switch statement, is also used with loops. A </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> statement causes an immediate exit from the innermost Switch, While, Do-While, or For statement in which it appears. Notice the word <i>innermost.</i> If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> is in a loop that is nested inside another loop, control exits the inner loop but not the outer.</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">One of the more common ways of using </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> with loops is to set up an infinite loop and use If tests to exit the loop. Suppose we want to input 10 pairs of integers, performing data validation and computing the square root of the sum of each pair. For data validation, assume that the first number must be less than 100 and the second must be greater than 50. Also, after each input, we want to test the state of the stream for EOF. Here's a loop using </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> statements to accomplish the task (assume </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TRUE</font><font face="Times New Roman, Times, Serif" size="3"> has been defined as the integer 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="Courier New, Courier, Mono New, Courier, Mono" size="2">loopCount聽=聽1;<br />while聽(TRUE)<br />{<br />聽聽聽聽cin聽&gt;&gt;聽numl;<br />聽聽聽聽if聽(聽!cin聽||聽numl聽&gt;=聽100)<br />聽聽聽聽聽聽聽聽break;<br />聽聽聽聽cin聽&gt;&gt;聽num2;<br />聽聽聽聽if聽(聽!cin聽||聽num2聽&lt;=聽50)<br />聽聽聽聽聽聽聽聽break;<br />聽聽聽聽cout聽&lt;&lt;聽sqrt(float聽(numl聽+聽num2))聽&lt;&lt;聽endl;<br />聽聽聽聽loopCount++;<br />聽聽聽聽if聽(loopCount聽&gt;聽10)<br />聽聽聽聽聽聽聽聽break;<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">Note that we could have used a For loop to count from 1 to 10, breaking out of it as necessary. However, this loop is both count-controlled and eventcontrolled, so we prefer to use a While loop.</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 above loop contains three distinct exit points. Some people vigorously oppose this style of programming, as it violates the single-entry, singleexit philosophy we discussed with multiple returns from a function. Is there any advantage to using an infinite loop in conjunction with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3">? To answer this question, let's rewrite the loop without using </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> statements. The loop must terminate when </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">numl</font><font face="Times New Roman, Times, Serif" size="3"> is invalid or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">num2</font><font face="Times New Roman, Times, Serif" size="3"> is invalid or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">loopCount</font><font face="Times New Roman, Times, Serif" size="3"> exceeds 10. We'll use Boolean flags to signal invalid data in the While condition:</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_471.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_472</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_473.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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