212 字
1 分钟
goto语句
2023-07-13

跳转语句#

C语言的跳转语句主要包括continue,break,retuen,还有就是goto

goto语句#

goto语句是在所有跳转语句中最自由的一种,

但在大型工程和多人协作工程中并不推荐,原因就在于它太过于自由,会导致代码的可读性变得较差

但这也无法撼动goto语句的地位

合理的使用goto会大大简化代码,并且使程序逻辑更加清晰

什么是goto语句#

goto,又称无条件跳转语句,使用goto语句可以直接跳转到label标注处,其语法为goto lable;

示例#

#include<iostream>
using namespace std;
int main(){
for (int i = 1; i <= 10; ++ i){
printf("%d ", i);
if (i == 6){
goto ERA;
}
}
cout << "Before ending" << endl;
ERA:
cout << "end" << endl;
}

输出

Terminal window
1 2 3 4 5 6 end
评论由 Giscus 提供支持

goto语句
https://blog.erina.top/posts/goto语句/
作者
Erina Yip
发布于
2023-07-13
许可协议
CC BY-NC-SA 4.0