cpp 专题
专题目录
您的位置:cpp > cpp 专题 > C++ goto语句
C++ goto语句
作者:--    发布时间:2019-11-20

c++ goto语句也称为跳转语句。 它用于将控制转移到程序的其他部分。 它无条件跳转到指定的标签。

它可用于从深层嵌套循环或switch case标签传输控制。

c++ goto语句示例

下面来看看看c++中goto语句的简单例子。

#include <iostream>  
using namespace std;  
int main()  
{  
ineligible:    
    cout<<"you are not eligible to vote!\n";    
    cout<<"enter your age:\n";    
    int age;  
    cin>>age;  
    if (age < 18){    
        goto ineligible;    
    }    
    else    
    {    
        cout<<"you are eligible to vote!";     
    }
    return 0;
}

上面代码执行结果如下 -

you are not eligible to vote!
enter your age:
16
you are not eligible to vote!
enter your age:
7
you are not eligible to vote!
enter your age:
22
you are eligible to vote!

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册