goto语句

发布时间 2023-06-02 18:54:00作者: ERA_YES

跳转语句

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;
}

输出

1 2 3 4 5 6 end