关于C语言goto语句的使用问题 进明白人进来看下
(1)正确的程序#include<stdio.h>voidmain(){intdice=0;intx=0;inty=0;printf("\nEnterthenumberb...
(1)正确的程序
#include <stdio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
{
goto there;
there:
x = 10;
printf("\nThe dice is %d.",x);
}
else
{
goto here;
here:
y = 20;
printf("\nThe dice is %d.",y);
}
getch();
}
(2)错误的程序
#include <stdio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
there:
x = 10;
printf("\nThe dice is %d.",x);
here:
y = 20;
printf("\nThe dice is %d.",y);
getch();
}
我想问一下第2个程序的语法表达错在哪里了 请明白人讲解一下。谢谢了。
我用了
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
为什么还是会执行 here的语句呢? 展开
#include <stdio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
{
goto there;
there:
x = 10;
printf("\nThe dice is %d.",x);
}
else
{
goto here;
here:
y = 20;
printf("\nThe dice is %d.",y);
}
getch();
}
(2)错误的程序
#include <stdio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
there:
x = 10;
printf("\nThe dice is %d.",x);
here:
y = 20;
printf("\nThe dice is %d.",y);
getch();
}
我想问一下第2个程序的语法表达错在哪里了 请明白人讲解一下。谢谢了。
我用了
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
为什么还是会执行 here的语句呢? 展开
5个回答
展开全部
第二个代码执行完there的两句后,还会继续执行here的代码.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
getch();需要包含头文件#include <conio.h>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <conio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
there:
x = 10;
printf("\nThe dice is %d.",x);
goto end;
here:
y = 20;
printf("\nThe dice is %d.",y);
goto end;
end:
getch();
}
there时,,here一样要运行下去的。。。在设置一个goto 哈。。有点汇编的意思。。。。。不过,在c / c++中,不是很必要,不要用goto。。
#include <conio.h>
void main()
{
int dice = 0;
int x = 0;
int y = 0;
printf("\nEnter the number between 1 and 6:");
scanf("%d",&dice);
if((dice >= 1) && (dice <= 6))
goto there;
else
goto here;
there:
x = 10;
printf("\nThe dice is %d.",x);
goto end;
here:
y = 20;
printf("\nThe dice is %d.",y);
goto end;
end:
getch();
}
there时,,here一样要运行下去的。。。在设置一个goto 哈。。有点汇编的意思。。。。。不过,在c / c++中,不是很必要,不要用goto。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
尽量避免用goto
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询