📄 breakcontinuegoto.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/print.css" type="text/css">
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>break、continue、goto</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++ Gossip: break、goto、continue</a></h1>
<br>
break可以离开目前switch、for、while、do
while的区块,并前进至区块后下一个陈述句,在switch中主要用来结束陈述句进行至下一个case的比对,在for、while与do
while中,主要用于中断目前的回圈执行,如果break出现并不是内含在for、while回圈中或switch陈述中,则会发生编译错误,
break的例子我们之前看过不少,这边不再举例。 <br>
<br>
continue的作用与break类似,主要使用于回圈,所不同的是break会结束区块的执行,而continue只会结束接下来区块中的陈述句,并
跳回回圈区块的开头继续下一个回圈,而不是离开回圈,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int
i = 1; i < 10; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
if(i == 5) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout << "i = " << i
<< endl; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
这段程式会显示i = 1到4,因为当 i 等于5时就会执行break而离开回圈,再看下面这个程式: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int
i = 1; i < 10; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
if(i == 5) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
continue; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout << "i = " << i
<< endl; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
<br>
这段程式会显示1到4,与6到9,当 i
等于5时,会执行continue直接结束此次回圈,这次回圈cout该行并没有被执行,然后从区块开头头执行下一次回圈,所以5并没有被显示。 <br>
<br>
goto是一个很方便,但是最不被建议使用的语法,滥用它的话会破坏程式的架构、使得程式的逻辑难以trace,事实上,在完全不使用goto的情况下,
您也可以使用结构化的语法来撰写程式。<br>
<br>
goto可以在程式中任意跳跃,跳跃前必须先设定好目的地,跳跃时必须指定目的地,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">start:
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">.... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">.... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">goto
start;</span><br>
</div>
<br>
其中start就是goto目的地的一个标签(Label),后面使用冒号,标签可以出现在程式的任一个地方。 <br>
<br>
一个简单的例子是这样的:<br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() { <br> int input = 0; <br><br>begin: <br><br> cout << "输入一数:"; <br> cin >> input; <br><br> if(input == 0) <br> goto error; <br><br> cout << "100 / " << input <br> << " = " << static_cast<double>(100) / input <br> << endl; <br><br> return 0; <br><br>error: <br> cout << "除数不可为0" << endl; <br> goto begin;<br>}</pre>
<br>
执行结果:<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">输入一数:0<br>
除数不可为 0<br>
输入一数:10<br>
100 / 10 = 10</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
如果您输入0,则程式会跳至error标签然后显示错误讯息,并重新跳至begin标签,然后再执行一次提示与输入,想想看就知道了,多跳个几次之后,这
个程式的逻辑马上会让您变得混乱不堪。 <br>
<br>
这个程式还是这么写好一些: <br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() { <br> int input = 0; <br><br> while(true) { <br> cout << "输入一数:"; <br> cin >> input; <br><br> if(input != 0) { <br> cout << "100 / " <br> << input <br> << " = " <br> << static_cast<double>(100) / input <br> << endl; <br> <br> break; <br> } <br><br> cout << "除数不可为0"; <br> } <br> <br> return 0; <br>}<br></pre>
<br>
总之,最好先忘了goto的存在,多培养结构化程式设计的思考,习惯养成之后就不会再想要用到goto了。 <br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -