转载

[原]C语言及程序设计初步例程-38 用break和continue改变流程

#include <stdio.h> int main() {  int s=0, k=1;  while(1)  {   s+=k ;   if (k>=100)    break ;   k=k+1;  }  printf("s = %d/n", s);  return 0; } continue语句 #include <stdio.h> int main() {  int i;  for (i=1; i<=10 ; i++)  {   if (i%2==0)    continue;   printf("%d/n", i);  }  return 0; } 

趣例:豆豆的每一天

#include <stdio.h> int main() {  char cChioce;   do  {   printf("*  1. 吃饭/n");   printf("*  2. 睡觉/n");   printf("*  3. 打豆豆/n");   printf("*  0. 退出/n");   printf("*  请选择(0-3):");   scanf("%c", &cChioce);   //选择后的处理   if (cChioce=='1')    printf("我吃吃吃.../n");   else if (cChioce=='2')    printf("我睡觉觉.../n");   else if (cChioce=='3')    printf("我打打打.../n");   else if (cChioce=='0')    break;   else   {    printf("/007选择错误!/n");    continue;   }   printf("恭喜你完成了一项工作!/n");  }  while(1);  return 0; } 

未来理想的方案

#include <stdio.h> int main() {  char cChioce;  void eat();  void sleep();  void hitdoudou();  do  {   printf("*  1. 吃饭/n");   printf("*  2. 睡觉/n");   printf("*  3. 打豆豆/n");   printf("*  0. 退出/n");   printf("*  请选择(0-3):");   scanf("%c", &cChioce);   if (cChioce=='1')    eat();   else if (cChioce=='2')    sleep();   else if (cChioce=='3')    hitdoudou();   else if (cChioce=='0')    break;   else   {    printf("/007选择错误!/n");    continue;   }   printf("恭喜你完成了一项工作!/n");  }  while(1);  return 0; } void eat() {  printf("我吃吃吃... .../n"); } void sleep() {  printf("我睡觉觉... .../n"); } void hitdoudou() {  printf("我打打打... .../n"); } 
正文到此结束
Loading...