C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言continue语句

使用C语言的continue语句将程序控制带到循环的开始。 Continue语句在循环内跳过了一些代码行,并continue下一次迭代。它主要用于条件,以便我们可以针对特定条件跳过一些代码。

语法:

//loop statements
continue;
//some lines of the code which is to be skipped

continue语句示例1

#include<stdio.h>
void main ()
{
    int i = 0; 
    while(i!=10)
    {
        printf("%d", i); 
        continue; 
        i++;
    }
}
输出
infinite loop

continue语句示例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处continue。

带有内部循环的C Continue语句

在这种情况下,Ccontinue语句仅continue内部循环,而不continue外部循环。
#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处continue。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4