本文概述
- 继续语句示例1
- 继续语句示例2
- C带有内部循环的继续语句
句法:
//loop statements
continue;
//some lines of the code which is to be skipped
继续语句示例1
#include<
stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}
输出量
infinite loop
继续语句示例2
#include<
stdio.h>
int main(){
int i=1;
//initializing a local variable
//starting a loop from 1 to 10
for(i=1;
i<
=10;
i++){
if(i==5){//if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n", i);
}//end of for loop
return 0;
}
输出量
1
2
3
4
6
7
8
9
10
如你所见,控制台上未打印5,因为循环在i == 5处继续。
C带有内部循环的继续语句在这种情况下,C继续语句仅继续内部循环,而不继续外部循环。
#include<
stdio.h>
int main(){
int i=1, j=1;
//initializing a local variable
for(i=1;
i<
=3;
i++){
for(j=1;
j<
=3;
j++){
if(i==2 &
&
j==2){
continue;
//will continue loop of j only
}
printf("%d %d\n", i, j);
}
}//end of for loop
return 0;
}
输出量
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
如你所见,控制台上未打印2 2,因为内部循环在i == 2和j == 2处继续。