在本教程中,我们将详细讨论 C++ 中的 break 语句和 C++ 中的 continue 语句,并提供合适的示例。
在学习 C++ 中的 break 语句和 continue 语句之前,请确保您熟悉以下内容:
在 C++ 语言的 32 个关键字中,`break` 是循环中最常用的关键字。当关键字 break 后跟分号时,就形成了 break 语句。break 语句用于立即中断循环执行序列。其原型如下:
当 C++ 中遇到 `break` 语句时,循环将终止。
`break` 语句的语法如下:
break;
break 语句主要与循环中的 if 语句和 switch case 一起使用。最初,我们可以追踪 break 语句如何在循环中工作以中断循环控制。

现在让我们讨论 break 语句在不同类型循环中的作用。
为了更清楚地说明,请查看下面的代码片段,并观察当遇到 break 语句时 for 循环控制是如何终止的。
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 7; i++) {
// break condition
if (i == 4) {
break;
}
cout << i << endl;
}
return 0;
}
输出
1 2 3
前面的程序中使用 for 循环在每次迭代后打印 i 的值。请注意代码:
if (i == 4)
{
break;
}
这意味着当 **i** 等于 4 时,break 语句结束循环。因此,输出不包含任何大于或等于 3 的值。
让我们编写一个程序来计算正数的和,假设如果用户输入负数,则循环中断,并且当输入负数时,它不会加到总和中。
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
while (true) {
// take input from the user
cout << " Please enter a number: ";
cin >> number;
// break condition
if (number < 0) {
break;
}
// add all positive numbers
sum += number;
}
// display the sum
cout << "The sum is " << sum << endl;
return 0;
}
输出
Please enter a number: 5 Please enter a number: 10 Please enter a number: 20 Please enter a number: 1 Please enter a number: 4 Please enter a number: -8 The sum is 40
用户在上述程序中输入一个数字。然后使用 while 循环打印用户输入的数字的总和。请注意这里的代码:
if (number < 0) {
break;
}
当用户输入负数时,break 语句终止循环,并执行循环外的代码。
除非用户输入负数,否则 while 循环将无限期地继续下去。
当与嵌套循环一起使用时,**Break** 会终止内部循环。
例如:
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// nested for loops
// first loop
for (int i = 1; i <= 4; i++) {
// second loop
for (int j = 1; j <= 4; j++) {
if (i == 3) {
break;
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
输出
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 2, j = 4 i = 4, j = 1 i = 4, j = 2 i = 4, j = 3 i = 4, j = 4
当 i == 3 时,前述程序中的 break 语句被执行。它结束内部循环,并将程序的控制流转移到外部循环。
因此,值 i=3 从未在输出中显示。
switch 语句和 break 语句一起使用。更多信息请参见 C++ switch 语句教程。
continue 语句在计算机编程中用于跳过循环的当前迭代,并将特定程序的控制转移到下一次迭代。
continue 语句的语法如下:
continue;

for 循环中的 continue 会跳过当前迭代并跳转到更新表达式。
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 4; i++) {
// condition to continue
if (i == 2) {
continue;
}
cout << i << endl;
}
return 0;
}
输出
1 3 4
while 循环中的 continue 将跳过当前迭代,并将程序的控制流返回到 while 条件。
现在让我们编写一个程序,只计算直到 30 的正数,假设如果用户输入一个负数,那么该特定数字将从计算中跳过。
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int number = 0;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
// continue condition
if (number > 30) {
cout << "The number is greater than 30 and won't be calculated." << endl;
number = 0; // the value of number is made 0 again
continue;
}
}
// display the sum
cout << "The sum is " << sum << endl;
return 0;
}
输出
Output 1 Enter a number: 14 Enter a number: 8 Enter a number: -9 The sum is 22 Output 2 Enter a number: 12 Enter a number: 0 Enter a number: 7 Enter a number: 89 The number is greater than 30 and won't be calculated. Enter a number: -3 The sum is 19
用户在上述程序中输入一个数字。while 循环用于打印用户输入的且不大于 30 的正数的总和。
注意 continue 语句。
if (number > 30) {
continue;
}
假设当用户输入一个大于 30 的数字时,continue 语句将跳过当前迭代。然后程序的控制流将移到 while 循环条件。
当用户输入一个小于零的数字时,循环结束。
当与嵌套循环一起使用时,continue 将跳过内部循环的当前迭代。例如:
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// nested for loops
// first loop
for (int i = 1; i <= 3; i++) {
// second loop
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
输出
i = 1, j = 1 i = 1, j = 3 i = 2, j = 1 i = 2, j = 3 i = 3, j = 1 i = 3, j = 3
当执行前面程序中的 continue 语句时,内部循环中的当前迭代将被跳过。程序的控制权转移到内部循环的更新表达式。
因此,值 **j = 2** 从未在输出中显示。
需要记住的要点
`break` 语句总是完全结束循环。而 `continue` 语句只跳过当前迭代。