c语言goto语句的使用

161 篇文章 10 订阅
#include <stdio.h>

//goto实现循环
void test001()
{
    int i,sum=0;
    i=1;
    loop:
    if(i<=100)
    {
        sum=sum+i;
        i++;
        goto loop;
    }
    printf("%d\n",sum);
}

void  test002()
{

    int i = 1,sum = 0;
    if (i == 10)
        goto loop;
any:
    if (i <= 100)
    {
        sum += i;
        i++;
        goto any;
    }
    //循环执行完成 继续执行下面
loop:
    printf("%d\n",sum);
}

int test003()
{
    int a=60;
    scanf("%d",&a);
    if(a>=90)
    {
        goto score_90;
    }else if(a<90 && a>=60)
    {
        goto score_60;
    }else{
        goto score_10;
    }

score_90:
    printf("优秀!\n");
    return 0;
score_60:
    printf("良好!\n");
    return 0;
score_10:
    printf("不及格!\n");
    return 0;
}

void  test004()
{
    int a,b,c=0;
loop:
    scanf("%d %d",&a,&b);
    if(a>b)
    {
        goto loop1;
    }
    else{
        printf("%d<%d\n",a,b);
        goto loop;
    }
loop1:
    c=b;
    b=a;
    a=c;
    printf("a=%d b=%d\n",a,b);
}

//c语言goto语句的使用
//goto和汇编语句比较像 使用ida动态调试一下
//goto语句也称为无条件转移语句
int main(void){

    test001();

    test002();
    test003();
    test004();
    return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值