goto语句包括两个部分:goto和一个标签名称
goto point1;为使goto语句工作,程序中必须包含由point1标签定位的其他语句
point1: printf("hello world! \n"); // 标签 + 冒号 + 一条语句示例代码:
/* test of goto */
#include <stdio.h>
int main(void)
{
    int num;
    int i;
    int sum = 0;
    printf("please enter a number: \n");
    scanf("%d", &num);
    for(i = 0; i <= num; i++)
    {
        sum += i;
        if(sum > 1000)
        {
            goto err; //数据超限,直接跳出循环
        }
        else
        {
            printf("current calculation result is %d \n", sum); // 每次循环打印一次
        }
        
    }
    err: printf("error: number out of range \n"); // 相应的处理程序
    
    return 0;
}运行结果 :

一、重要原则:C中避免使用goto语句
原则上,C程序根本不需要使用goto语句
1)if语句、if-else语句、while语句、continue语句、break语句可以在不同的应用场景中代替goto的功能
2)不要在程序中使用goto语句随意跳转到程序的不同部分
3)允许的goto:在出现故障时从一组嵌套的循环中跳出(因为单条的break语句仅能跳出最里层的循环,示例:
...
while(a)
{
    for(i = 0; i < num; i++)
    {
        for(j = 0; j < num; j++)
        {
            statement1
            if(bit error) // 判定程序出错
            {
                goto error_function; // 使用goto语句直接跳转到相应的处理程序
            }
            statement2;
        }
        statement3;
    }
    statement4;
}
statement5;
error_function: function(); // 处理程序
...二、程序跳转指令总结:
break指令
可以与3中循环形式(while、for、do-while)中的任何一种以及switch语句一起使用
功能:令程序跳过包含它的循环或switch语句的剩余部分,继续执行紧跟在循环或switch后的下一条命令
continue指令
可以与3种循环形式(while、for、do-while)中的任何一种一起使用,不能和switch语句一起使用
功能:令程序跳过循环中的剩余语句;对于while或for循环,开始下一个循环周期;对于do-while循环,退出条件进行判断,如果必要则开始下一个循环周期
goto语句
功能:令程序直接跳转到由指定标签定位的语句;使用冒号将被标记的语句和其标签分隔;标签遵循变量的命名规则;被标记的语句可以出现在goto之前或之后
选择语句是开发具有智能行为程序的基础
在C中,if、if-else、switch语句,连同调减运算符(? :)一起实现了选择


![[NOIP2017 提高组] 奶酪(C++,并查集)](https://img-blog.csdnimg.cn/img_convert/927c8f7cef70a66d2cfd9b3eeaf88ac1.png)
















