C语言教程:goto语句(无条件跳转到其他标签)

2018-10-0608:26:20编程语言入门到精通Comments5,279 views字数 504阅读模式

goto语句被称为C语言中的跳转语句。用于无条件跳转到其他标签。它将控制权转移到程序的其他部分。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

goto语句一般很少使用,因为它使程序的可读性和复杂性变得更差。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

语法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

goto label;
C

goto语句示例

让我们来看一个简单的例子,演示如何使用C语言中的goto语句。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

打开Visual Studio创建一个名称为:goto的工程,并在这个工程中创建一个源文件:goto-statment.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

#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");
    }

}
C

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html

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!
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6405.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6405.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定