文章目录
- 1. break
- 2. continue 语句
- 3. goto 语句
- goto的存在
- 4. 跳出多重循环
- 4.1 goto 直接跳转
- 4.2 C++11及其后版本的 `return` 语句
- 4.3 使用标志变量
在C++中,控制语句用于管理程序的执行流程。常见有 break、continue 和 goto。
1. break
break语句主要用于在循环或者switch语句中中断当前执行的流程,并跳出循环或switch结构。
用法:
- 在switch语句中,从一个case分支中直接跳出 switch,避免执行后续的case分支
- 在循环结构中,立即结束循环,跳出当前所在的循环体。也就是说在一个双重循环(或者更多层嵌套的循环)中使用break,它只能终止内层的循环,而不会终止外层的循环。
例如,跳出 switch 结构
int num = 2;
switch (num) {
case 1:
cout << "Number is 1" << endl;
break; // 跳出switch
case 2:
cout << "Number is 2" << endl;
break; // 跳出switch
case 3:
cout << "Number is 3" << endl;
break; // 跳出switch
default:
cout << "Number is not 1, 2 or 3" << endl;
}
例如,跳出当前循环体
#include <iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; //i等于5时,break终止循环,跳出for循环
}
cout << i << " ";
}
return 0;
}
// 控制台输出内容为 1 2 3 4
再如,双重循环,代码如下所示。在这个例子中,当 j == 3 时,break语句会终止内层的for循环,但外层的for循环(即 i 的循环)会继续执行
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 3) {
break; // 只能跳出内层的for循环
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
2. continue 语句
跳过当前这次循环的剩余代码,立即开始下一次循环,continue不会终止循环
例如:
#include <iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // 当i等于5时提前结束本次循环,开始下一次循环
}
cout << i << " ";
}
return 0;
}
// 控制台输出为 1 2 3 4 6 7 8 9 10
3. goto 语句
- 无条件地从一个地方跳到程序中的另一个任意位置,会降低代码的可读性和可维护性,不建议使用
- 定义一个标签(label),然后通过goto语句跳转到该标签处执行代码
- 标签以冒号:结尾,并放在代码的某一行上
例如:
#include <iostream>
using namespace std;
int main()
{
int n = 0;
loop_start:
cout << "请输入一个数字(输入 0 退出):";
cin >> n;
if (n != 0)
{
cout << "你输入的是:" << n << endl;
goto loop_start;
}
cout << "GoodBye!" << endl;
return 0;
}
goto的存在
尽管不推荐goto
语句,但它仍然存在于C++语言中。历史原因,C++来源于C,C从汇编和早期的编程语言中演化而来。早期语言中,goto是实现控制流的主要方式之一。在某些旧的代码库或者嵌入式设备中的固件中,可能大量使用了goto语句,为了兼容性保留,能够维护和扩展,不必重写大量逻辑。
也可以用于跳出嵌套较深的情况。见后面的4. 跳出多重循环。
goto在某些情况下还可以用于处理资源释放或者异常处理,有时候在一些嵌入式系统或者需要高性能的应用中,直接用goto进行异常处理或者资源清理可能会更加简洁。例如:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
file.open("example.txt");
if (!file.is_open()) {
goto error; // 如果文件打开失败,跳到错误处理部分
}
// 文件操作代码
// ...
file.close();
return 0;
error:
cout << "Error opening file!" << endl;
return 1;
}
在这个例子中,如果文件打开失败,程序会直接跳转到error
部分,执行错误处理逻辑。虽然这种方式不如C++中的try-catch
异常处理机制规范,但在某些对性能要求极高或资源有限的场合,比如嵌入式开发、驱动程序编写领域等,这种方式提供了一种相对低开销的替代方案,可能会更好。
4. 跳出多重循环
4.1 goto 直接跳转
例如:
#include <iostream>
using namespace std;
int main()
{
bool found = false;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i * j == 50) {
found = true;
goto end_loop; // 跳出双重循环
}
}
}
end_loop:
if (found) {
cout << "Found the pair!" << endl;
} else {
cout << "Pair not found!" << endl;
}
return 0;
}
4.2 C++11及其后版本的 return
语句
可以使用 return 直接结束函数的执行,函数都结束了,从而循环结束了,也就是说自动跳出所有嵌套的循环。例如
#include <iostream>
using namespace std;
void processLoop()
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (j == 3)
{
return ;//直接结束函数体,从而跳出循环
}
cout << "i = " << i << ", j = " << j << endl;
}
}
cout << "j==3时,提前退出所有循环" << endl;
//不会执行,因为return提前结束了函数体
}
int main()
{
processLoop(); //调用函数
cout << "循环在 j 为 3 时提前结束" << endl; //控制台输出
return 0;
}
这里,如果 return 语句放在了 main 函数中,在 j == 3 时,则会直接结束整个程序的执行。
与前面放在普通函数里有所不同,普通函数因为return结束了自身的执行,还可以返回到主调函数中。
然而,在main函数中,return 则意味着程序完全终止,即程序立即退出。
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (j == 3)
{
return 0;
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
4.3 使用标志变量
用标志变量来标记是否需要跳出所有循环,而不是直接使用 return 结束程序。
#include <iostream>
using namespace std;
int main()
{
int i, j;
bool flag = false; //起到标志作用的变量
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (j == 3)
{
flag = true; //设置标志
break; //先跳出内层循环
}
cout << "i = " << i << ", j = " << j << endl;
}
if (flag) //判断标志
{
break; //跳出外层循环
}
}
cout << "退出了双重循环" << endl;
return 0;
}