c语言goto为什么最好不要用 您所在的位置:网站首页 c语言中有goto语句吗 c语言goto为什么最好不要用

c语言goto为什么最好不要用

2023-07-18 01:56| 来源: 网络整理| 查看: 265

什么是goto语句

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

goto语句一般很少使用,因为它使程序的可读性和复杂性变得更差。

语法

goto label;

goto语句示例

让我们来看一个简单的例子,演示如何使用C语言中的goto语句。

打开Visual Studio创建一个名称为:goto的工程,并在这个工程中创建一个源文件:goto-statment.c,其代码如下所示 -

#include

void main() {

int age;

gotolabel:

printf("You are not eligible to vote!n");

printf("Enter you age:n");

scanf("%d", &age);

if (age < 18) {

goto gotolabel;

}else {

printf("You are eligible to vote!n");

}

}

执行上面代码,得到以下结果

You are not eligible to vote!

Enter you age:

12

You are not eligible to vote!

Enter you age:

18

You are eligible to vote!

为什么它这么不受待见?



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有