C++ Goto语句

C++ Goto语句 首页 / C++入门教程 / C++ Goto语句

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

它可用于从深层嵌套的循环或开关案例标签转移控制权。

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

输出:

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!

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Nginx核心知识150讲 -〔陶辉〕

程序员的数学基础课 -〔黄申〕

Swift核心技术与实战 -〔张杰〕

设计模式之美 -〔王争〕

数据中台实战课 -〔郭忆〕

Go 语言项目开发实战 -〔孔令飞〕

网络排查案例课 -〔杨胜辉〕

云计算的必修小课 -〔吕蕴偲〕

AI大模型之美 -〔徐文浩〕

好记忆不如烂笔头。留下您的足迹吧 :)