C循环例子

while循环

#include<stdio.h>

int main(void){
    int a = 10;
    while (a<20)
    {
        printf("%d\n",a);
        a++;
    }
    return 0;
}

for循环

#include<stdio.h>

int main(void){
   for (int i = 0; i <= 10; i++)
   {
       /* code */
       printf("%d\n",i);
   }
   return 0;
}

do...while循环

#include<stdio.h>

int main(void){
   int a = 10;
   //do while循环,do语句会先执行一次
   do
   {
       printf("%d\n",a);
       a+=1;
   } while (a<20);
   
}

break

#include<stdio.h>

int main(void){
   int a = 10;
   while(a<20){
       a++;
       printf("%d\n",a);
       if (a==15)
       {
           break;//直接跳出循环
       }
       
   }
   return 0;
}

 continue

C 语言中的 continue 语句有点像 break 语句。但它不是强制终止,continue 会跳过当前循环中的代码,强迫开始下一次循环。

C循环例子

 

#include<stdio.h>

int main(void){
   int a = 10;
  do
  {
      if (a==15)
      {
          a = a + 1;
          continue;
      }
      printf("%d\n",a);
      a++;
      
  } while (a<20);
  
   return 0;
}

上一篇:git 推送更新出现错误: Updates were rejected because the remote contains work that you do


下一篇:220113