Rumah > pembangunan bahagian belakang > tutorial php > php中关于break与continue两者之间的用法区别

php中关于break与continue两者之间的用法区别

黄舟
Lepaskan: 2023-03-11 11:16:02
asal
2072 orang telah melayarinya

一般说来,程序进入循环体后在下次循环判断之前执行循环体里的所有语句,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;
}
Salin selepas log masuk
$ 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 !!!
$
Salin selepas log masuk

嵌套循环(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;
}
Salin selepas log masuk
$ 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 !!!
$
Salin selepas log masuk

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;
}
Salin selepas log masuk
$ 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 !!!
$
Salin selepas log masuk

嵌套循环(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;
}
Salin selepas log masuk
$ 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 !!!
$
Salin selepas log masuk


Atas ialah kandungan terperinci php中关于break与continue两者之间的用法区别. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan