c语言有4个跳转语句return、goto、break、continue
2011-02-23 17:46
671 查看
c语言有4个跳转语句return、goto、break、continue:
其中return、goto可以应用到程序的任何地方,break、continue和循环语句一起使用。
1、return
return用于从函数(不管是在while中调用还是for循环中调用)返回,使执行的函数返回到函数的调用点。
return的一般形式是:
return
表达式
其中表达式就是函数返回的值。
函数使用return的次数不受限制,但是当函数遇到第一个return的时候,函数停止执行,返回到函数的调用点。
声明为void型的函数不能包括代值的return语句。
例如递归函数:
int
digui(int num);
void
main(void)
{
int
num=0;
int
fac=0;
printf("input a number
/n");
scanf("%d",&num);
fac=digui(num);
printf("%d /'s factorial is
%d/n",num,fac);
getch();
}
int digui(int
num)
{
if(num==1) return
1;
else return
num*digui(num-1);
}
程序执行结果是:
input a number
5
5 's factorial is
120
我们可以看到main函数是void类型,不需要return语句。而digui函数是需要return的。
在递归函数中我们有两个return。if(num==1)
return 1;这条语句是递归函数的终结。
2、goto(注意使用goto调到的指定标签必须是同在一个函数中,goto无法进行跨函数调用标签)
基本上已经不再使用goto语句了。
基本形式:
goto
标签;
标签:
我们使用goto构造一个循环,例如:
int main(void)
{
int
i=0;
Electrics:
printf("%d
",i);
i++;
if(i<10)
goto Electrics;
getch();
}
3、break
在循环中break终止循环,也就是跳出循环(注,continue是跳过剩余代码,执行下一个循环)。执行循环下边的语句。
在switch中终止一个case。
例如:
int
main(void)
{
int
t=0;
for(;t<=10;t++)
{
if(t==9)break;
printf("%d
",t);
}
printf("/n");
printf("hello
c!");
getch();
}
程序结果:
0 1 2 3 4 5 6 7
8
hello
c!
当执行到t==9的时候,退出循环,执行printf("/n");语句。所以没有输出10。
当是嵌套的多层循环,break只是退出最内层的循环。
int
main(void)
{
int
t=0;
for(;t<=2;t++)
{
int
j=0;
for
(;j<5;j++)
{
if(j>2)break;
printf("j is %d
/n",j);
}
printf("t is %d
/n",t);
}
printf("/n");
printf("hello
c!");
getch();
}
程序结果:
j is 0
j is
1
j is 2
t is 0
j is 0
j is 1
j is 2
t is 1
j is 0
j is
1
j is 2
t is 2
从程序结果可以看出,break每次都是终止内层循环。和外层循环没有关系。
hello
c!
4、continue
简单的说就是跳过剩余代码(而break是跳出循环),执行下一个循环。
例如:
int
main(void)
{
int
t=0;
for(;t<=10;t++)
{
if(t==9)continue;
printf("%d
",t);
}
printf("/n");
printf("hello
c!");
getch();
}
将上面的程序中break改成continue,我们看看结果。
0
1 2 3 4 5 6 7 8 10
hello
c!
当t==9的时候,程序没有执行下面的输出语句,直接到t++;执行下一个循环。所以没有输出9,但是输出了10。
转载请注明来自秦工的博客,本文地址:http://www.arm32.com/post/10954.html
其中return、goto可以应用到程序的任何地方,break、continue和循环语句一起使用。
1、return
return用于从函数(不管是在while中调用还是for循环中调用)返回,使执行的函数返回到函数的调用点。
return的一般形式是:
return
表达式
其中表达式就是函数返回的值。
函数使用return的次数不受限制,但是当函数遇到第一个return的时候,函数停止执行,返回到函数的调用点。
声明为void型的函数不能包括代值的return语句。
例如递归函数:
int
digui(int num);
void
main(void)
{
int
num=0;
int
fac=0;
printf("input a number
/n");
scanf("%d",&num);
fac=digui(num);
printf("%d /'s factorial is
%d/n",num,fac);
getch();
}
int digui(int
num)
{
if(num==1) return
1;
else return
num*digui(num-1);
}
程序执行结果是:
input a number
5
5 's factorial is
120
我们可以看到main函数是void类型,不需要return语句。而digui函数是需要return的。
在递归函数中我们有两个return。if(num==1)
return 1;这条语句是递归函数的终结。
2、goto(注意使用goto调到的指定标签必须是同在一个函数中,goto无法进行跨函数调用标签)
基本上已经不再使用goto语句了。
基本形式:
goto
标签;
标签:
我们使用goto构造一个循环,例如:
int main(void)
{
int
i=0;
Electrics:
printf("%d
",i);
i++;
if(i<10)
goto Electrics;
getch();
}
3、break
在循环中break终止循环,也就是跳出循环(注,continue是跳过剩余代码,执行下一个循环)。执行循环下边的语句。
在switch中终止一个case。
例如:
int
main(void)
{
int
t=0;
for(;t<=10;t++)
{
if(t==9)break;
printf("%d
",t);
}
printf("/n");
printf("hello
c!");
getch();
}
程序结果:
0 1 2 3 4 5 6 7
8
hello
c!
当执行到t==9的时候,退出循环,执行printf("/n");语句。所以没有输出10。
当是嵌套的多层循环,break只是退出最内层的循环。
int
main(void)
{
int
t=0;
for(;t<=2;t++)
{
int
j=0;
for
(;j<5;j++)
{
if(j>2)break;
printf("j is %d
/n",j);
}
printf("t is %d
/n",t);
}
printf("/n");
printf("hello
c!");
getch();
}
程序结果:
j is 0
j is
1
j is 2
t is 0
j is 0
j is 1
j is 2
t is 1
j is 0
j is
1
j is 2
t is 2
从程序结果可以看出,break每次都是终止内层循环。和外层循环没有关系。
hello
c!
4、continue
简单的说就是跳过剩余代码(而break是跳出循环),执行下一个循环。
例如:
int
main(void)
{
int
t=0;
for(;t<=10;t++)
{
if(t==9)continue;
printf("%d
",t);
}
printf("/n");
printf("hello
c!");
getch();
}
将上面的程序中break改成continue,我们看看结果。
0
1 2 3 4 5 6 7 8 10
hello
c!
当t==9的时候,程序没有执行下面的输出语句,直接到t++;执行下一个循环。所以没有输出9,但是输出了10。
转载请注明来自秦工的博客,本文地址:http://www.arm32.com/post/10954.html
相关文章推荐
- 跳转语句:goto;break;continue;return
- C#参考 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C语言:break、continue、return在switch语句中的跳转关系
- [引]C#参考跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- [引]C#参考 跳转语句(break,continue,goto,return,throw)
- [引]C#参考 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C语言中break,continue,goto,return语句区别
- C# 跳转语句(break,continue,goto,return,throw)
- 跳转语句的使用(break,continue,goto,return)
- 【转】C# 跳转语句(break,continue,goto,return,throw)
- C# 跳转语句(break,continue,goto,return,throw)
- C#参考 跳转语句(break,continue,goto,return,throw)
- C语言 转移语句:goto语句,break语句和continue语句
- break, continue, goto, return语句详解