break与continue的用法区别

一般说来,程序进入循环体后在下次循环判断之前执行循环体里的所有语句,break和continue语句可以终止循环或忽略某些循环。

break: 此语句导致程序终止包含它的循环,并进行程序的下一阶段(整个循环后面的语句),即,不是跳到下一个循环周期而是退出循环。如果break语句包含在嵌套循环里,它只跳出最里面的循环。

#include<stdio.h>
int main()
{
int res=0;
int i=0;
int n=0;
printf("test break and continue\n");
for(i=0;i<6;i++)
{
printf("start...\n");
if(i==3)
        {
        printf("break \n");
        break;
        printf("break after\n");
        //printf("continue \n");
        //continue;
        //printf("continue after\n");
        }


printf("now i = %d\n",i);
}
printf("test over !!!\n");
return 0;
}
$ gcc -o bc bc.c
$ ./bc
test break and continue
start...
now i = 0
start...
now i = 1
start...
now i = 2
start...
break 
test over !!!
$ 
嵌套循环(break)
#include<stdio.h>
int main()
{
int res=0;
int i=0;
int n=0;
printf("test break and continue\n");
for(i=0;i<6;i++)
{
printf("start...\n");
for(n=0;n<6;n++)
{
if(n==3)
        {
        printf("break \n");
        break;
        //printf("continue \n");
        //continue;
        }
printf("now n= %d\n",n);
}
if(i==3)
        {
        printf("break \n");
        break;
        printf("break after\n");
        //printf("continue \n");
        //continue;
        //printf("continue after\n");
        }


printf("now i = %d\n",i);
}
printf("test over !!!\n");
return 0;
}
$ gcc -o bc bc.c
$ ./bc
test break and continue
start...
now n= 0
now n= 1
now n= 2
break 
now i = 0
start...
now n= 0
now n= 1
now n= 2
break 
now i = 1
start...
now n= 0
now n= 1
now n= 2
break 
now i = 2
start...
now n= 0
now n= 1
now n= 2
break 
break 
test over !!!
$ 
continue: 循环语句里有此语句时,程序运行到此语句时,不在执行循环体里continue后面的语句而是跳到下一个循环入口处执行下一个循环。如果continue语句包含在嵌套循环语句里,它只影响包含它的最里层的循环。

#include<stdio.h>

int main()
{
int res=0;
int i=0;
int n=0;
printf("test break and continue\n");
for(i=0;i<6;i++)
{
printf("start...\n");
if(i==3)
        {
        //printf("break \n");
        //break;
        //printf("break after\n");
        printf("continue \n");
        continue;
        printf("continue after\n");
        }

printf("now i = %d\n",i);
}
printf("test over !!!\n");
return 0;
}
$ gcc -o bc bc.c
$ ./bc
test break and continue
start...
now i = 0
start...
now i = 1
start...
now i = 2
start...
continue 
start...
now i = 4
start...
now i = 5
test over !!!
$
嵌套循环(continue):

#include<stdio.h>

int main()
{
int res=0;
int i=0;
int n=0;
printf("test break and continue\n");
for(i=0;i<6;i++)
{
printf("start...\n");

for(n=0;n<6;n++)
{
if(n==3)
        {
        //printf("break \n");
        //break;
        printf("continue \n");
        continue;
        }
printf("now n= %d\n",n);
}

if(i==3)
        {
        //printf("break \n");
        //break;
        //printf("break after\n");
        printf("continue \n");
        continue;
        printf("continue after\n");
        }

printf("now i = %d\n",i);
}
printf("test over !!!\n");
return 0;
}
$ gcc -o bc bc.c
$ ./bc
test break and continue
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
now i = 0
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
now i = 1
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
now i = 2
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
continue 
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
now i = 4
start...
now n= 0
now n= 1
now n= 2
continue 
now n= 4
now n= 5
now i = 5
test over !!!
$ 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值