C语言goto语句
作者:--
发布时间:2019-11-20
评论:0
阅读:0
goto语句被称为c语言中的跳转语句。用于无条件跳转到其他标签。它将控制权转移到程序的其他部分。
goto语句一般很少使用,因为它使程序的可读性和复杂性变得更差。
语法
goto label;
goto语句示例
让我们来看一个简单的例子,演示如何使用c语言中的goto语句。
打开visual studio创建一个名称为:goto的工程,并在这个工程中创建一个源文件:goto-statment.c,其代码如下所示 -
#include <stdio.h>
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!