c语言goto用法例程

c语言goto用法例程

#include<stdio.h>

	int main()
	{
	char a,b;
	int n1=1,n2=2;
	printf("请输入y或x\n"); 
	a=getchar();//a接收数据
	b=getchar();//b既收\n
	while(a!='\n')
	{
	if(a=='y')
		goto f1;
	else if(a=='n')
		goto f2;
	else
		{
		printf("请输入y或x\n");
		//此处将回车也输入了一次  
		a=getchar(); 
		b=getchar();
		}
	}
	
f1:	printf("%d\n",n1);
	goto f3;//没有本条语句会打印c的值 
f2:	printf("%d\n",n2);
f3:	getchar();
	getchar();
	return 0;
	}

测试y
在这里插入图片描述
测试n
在这里插入图片描述直接按回车在这里插入图片描述

发布了10 篇原创文章 · 获赞 0 · 访问量 458

猜你喜欢

转载自blog.csdn.net/KEY_Init/article/details/103337530