break 语句
break 语句可终止执行最近的封闭循环或其所在条件语句。 控制权将传递给该语句结束之后的语句(如果有的话)。
|
1
|
break;
|
备注
break 语句与 switch 条件语句以及 do、for 和 while 循环语句配合使用。
在 switch 语句中,break 语句将导致程序执行 switch 语句之外的下一语句。 如果没有 break 语句,则将执行从匹配的 case 标签到 switch 语句末尾之间的每个语句,包括 default 子句。
在循环中,break 语句将终止执行最近的 do、for 或 while 封闭语句。 控制权将传递给终止语句之后的语句(如果有的话)。
在嵌套语句中,break 语句只终止直接包围它的 do、for、switch 或 while 语句。 你可以使用 return 或 goto 语句从较深嵌套的结构转移控制权。
示例
以下代码演示如何在 for 循环中使用 break 语句。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
using namespace std;
int main()
{
// An example of a standard for loop
for (int i = 1; i < 10; i++)
{
cout << i << '\\n';
if (i == 4)
break;
}
// An example of a range-based for loop
int nums []{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i : nums) {
if (i == 4) {
break;
}
cout << i << '\\n';
}
}
|
在每个用例中:
|
1
2
3
|
1
2
3
|
以下代码演示如何在 while 循环和 do 循环中使用 break。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
cout << i << '\\n';
i++;
}
i = 0;
do {
if (i == 4) {
break;
}
cout << i << '\\n';
i++;
} while (i < 10);
}
|
在每个用例中:
|
1
2
3
4
|
0
1
2
3
|
以下代码演示如何在 switch 语句中使用 break。 如果你要分别处理每个用例,则必须在每个用例中使用 break;如果不使用 break,则执行下一用例中的代码。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include <iostream>
using namespace std;
enum Suit{ Diamonds, Hearts, Clubs, Spades };
int main() {
Suit hand;
. . .
// Assume that some enum value is set for hand
// In this example, each case is handled separately
switch (hand)
{
case Diamonds:
cout << "got Diamonds \\n";
break;
case Hearts:
cout << "got Hearts \\n";
break;
case Clubs:
cout << "got Clubs \\n";
break;
case Spades:
cout << "got Spades \\n";
break;
default:
cout << "didn't get card \\n";
}
// In this example, Diamonds and Hearts are handled one way, and
// Clubs, Spades, and the default value are handled another way
switch (hand)
{
case Diamonds:
case Hearts:
cout << "got a red card \\n";
break;
case Clubs:
case Spades:
default:
cout << "didn't get a red card \\n";
}
}
|
continue 语句
强制转移对最小封闭 do、for 或 while 循环的控制表达式的控制。
语法
|
1
|
continue;
|
备注
将不会执行当前迭代中的所有剩余语句。确定循环的下一次迭代,如下所示:
在 do 或 while 循环中,下一个迭代首先会重新计算 do 或 while 语句的控制表达式。
在 for 循环中(使用语法 for(init-expr; cond-expr; loop-expr)),将执行 loop-expr 子句。然后,重新计算 cond-expr 子句,并根据结果确定该循环结束还是进行另一个迭代。
下面的示例演示了如何使用 continue 语句跳过代码部分并启动循环的下一个迭代。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// continue_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
i++;
printf_s("在继续之前\\n");
continue;
printf("在继续之后,不被输出\\n");
} while (i < 3);
printf_s("在do循环之后\\n");
}
|
输出:
|
1
2
3
4
|
在继续之前
在继续之前
在继续之前
在do循环之后
|
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 50
-
2025-05-25 38
-
2025-05-27 45
-
2025-06-04 78
-
2025-06-04 84

