C语言基础知识-程序流程结构

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.概述

  1. C语言支持最基本的三种程序运行结构:顺序结构,选择结构,循环结构。
      顺序结构:程序按顺序执行,不发生跳转。
      选择结构:依据是否满足条件,有选择的执行相应功能。
      循环结构:依据条件是否满足,循环多次执行某段代码。

二.选择结构

1>.if语句

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int a = ;
  13. int b = ;
  14. if (a > b)
  15. {
  16. printf("a > b\n");
  17. }
  18.  
  19. return ;
  20. }
  21. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  22. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo if_demo.c
  23. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  24. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo
  25. a > b
  26. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

2>.if ... else语句

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo2.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int a = ;
  13. int b = ;
  14. if (a > b)
  15. {
  16. printf("a > b\n");
  17. }
  18. else
  19. {
  20. printf("a < b\n");
  21. }
  22.  
  23. return ;
  24. }
  25. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  26. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo2 if_demo2.c
  27. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  28. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo2
  29. a < b
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

3>.if ... else if ...else语句

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo3.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int a = ;
  13. int b = ;
  14. if (a > b)
  15. {
  16. printf("a > b\n");
  17. }
  18. else if(a == b)
  19. {
  20. printf("a == b\n");
  21. }
  22. else
  23. {
  24. printf("a < b\n");
  25. }
  26.  
  27. return ;
  28. }
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo3 if_demo3.c
  31. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo3
  33. a == b
  34. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

4>.三目运算符【其实其内部判断条件和if相似,语法结构简单明了

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo4.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int a = ;
  13. int b = ;
  14. int max;
  15.  
  16. if (a > b)
  17. {
  18. max = a;
  19. }
  20. else
  21. {
  22. max = b;
  23. }
  24. printf("s1 = %d\n",max);
  25.  
  26. a = ;
  27. b = ;
  28. max = (a > b ? a:b);      //上面一大堆代码,我们仅仅用三目运算符一行简写。三目运算符格式为"表达式?选项1[表达式]:选项2",即如果表达式为真,选择选项1的结果,如果为假则选择新选项2。
  29. printf("s2 = %d\n",max);
  30.  
  31. return ;
  32. }
  33. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  34. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo4 if_demo4.c
  35. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  36. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo4
  37. s1 =
  38. s2 =
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  40. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

5>.switch语句【注意:if 条件语句执行效率差,switch条件语句执行效率相对较高,但是if可以判断一个区间,而switch则只能用来判断一个值】

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat switch_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. char c;
  13. c = getchar(); //注意该方法只会接收第一个字符哟~比如你输入的是100,它只会接收第一个字符“1”
  14.  
  15. switch(c) //参数只能是整型变量
  16. {
  17. case '':
  18. printf("OK\n");
  19. break; //switch遇到break就中断了
  20. case '':
  21. printf("not OK\n");
  22. break;
  23. default: //如果上面的条件都不满足,那么执行default
  24. printf("are u OK?\n");
  25. }
  26.  
  27. return ;
  28. }
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o switch_demo switch_demo.c
  31. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo
  33.  
  34. OK
  35. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  36. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo
  37.  
  38. not OK
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo
  40.  
  41. are u OK?
  42. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  43. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo
  44.  
  45. are u OK?
  46. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

三.循环结构

1>.while语句

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat while_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int a = ;
  16. while(a < )
  17. {
  18. printf("a = %d\n",a);
  19. a++;
  20. system("sleep 0.5");
  21. }
  22. printf("程序执行完毕~\n");
  23.  
  24. return EXIT_SUCCESS;
  25. }
  26. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  27. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o while_demo while_demo.c
  28. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./while_demo
  30. a =
  31. a =
  32. a =
  33. a =
  34. a =
  35. a =
  36. a =
  37. a =
  38. a =
  39. 程序执行完毕~
  40. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

2>.do ... while语句

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat narcissus.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int index = ;
  16. do
  17. {
  18. int one = , ten=, hundred=; //将一个三位数分解个位,十位,百位
  19.  
  20. hundred = index / ; //百位
  21.  
  22. ten = index / % ; //十位
  23.  
  24. one = index % ; //个位
  25.  
  26. if (hundred * hundred * hundred + ten * ten * ten + one * one * one == index) //各个位数的立方和等于该数本身,那么它就是一个水仙花
  27. {
  28. printf("%d是水仙花数\n",index);
  29. }
  30.  
  31. index ++;
  32. }while(index < );
  33.  
  34. return EXIT_SUCCESS;
  35. }
  36. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  37. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o narcissus narcissus.c
  38. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./narcissus
  40. 153是水仙花数
  41. 370是水仙花数
  42. 371是水仙花数
  43. 407是水仙花数
  44. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

3>.for循环

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int index = ;
  16. int sum = ;
  17.  
  18. for(index = ;index<=;index++) //计算0-100之间所有数字的是总和
  19. {
  20. sum += index;
  21. }
  22.  
  23. printf("sum = %d\n",sum);
  24.  
  25. return EXIT_SUCCESS;
  26. }
  27. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  28. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo for_demo.c
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo
  31. sum =
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  33. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo2.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. for(int i = ;i<;i++)
  16. {
  17. int one = , ten=, hundred=; //将一个三位数分解个位,十位,百位
  18.  
  19. hundred = i / ; //百位
  20.  
  21. ten = i / % ; //十位
  22.  
  23. one = i % ; //个位
  24.  
  25. if (hundred * hundred * hundred + ten * ten * ten + one * one * one == i) //各个位数的立方和等于该数本身,那么它就是一个水仙花
  26. {
  27. printf("%d是水仙花数\n",i);
  28. }
  29. }
  30. return EXIT_SUCCESS;
  31. }
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  33. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo2 for_demo2.c -std=c99 #注意,在Linux系统我们编译for循环代码时需要指定"-std=c99",否则会报错"error: ‘for’ loop initial declarations are only allowed in C99 mode"
  34. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  35. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo2
  36. 153是水仙花数
  37. 370是水仙花数
  38. 371是水仙花数
  39. 407是水仙花数
  40. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

使用for循环打印三位数存在的水仙花数字

4>.嵌套循环(循环之间可以相互嵌套)

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_99.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int i,j;    #我们提前声明了变量i和j,如果我们在这里不声明直接在for循环里面声明也是可以的,只不过在Linux操作系统编译时,我们需要指定std的库为c99,默认使用的是c90库。否则会报错"error: ‘for’ loop initial declarations are only allowed in C99 mode"
  16. for(i = ; i <= ; i++)
  17. {
  18. for(j=; j<=i; j++)
  19. {
  20. printf("%d x %d = %d\t",i,j,i * j);
  21. }
  22. printf("\n");
  23. }
  24. return EXIT_SUCCESS;
  25. }
  26. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  27. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_99 for_99.c
  28. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_99
  30. x =
  31. x = x =
  32. x = x = x =
  33. x = x = x = x =
  34. x = x = x = x = x =
  35. x = x = x = x = x = x =
  36. x = x = x = x = x = x = x =
  37. x = x = x = x = x = x = x = x =
  38. x = x = x = x = x = x = x = x = x =
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  40. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

 5>.循环语句练习一(猜年龄)

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat guess_age.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10.  
  11. int main(void)
  12. {
  13. srand((unsigned int)time(NULL)); //加入随机数种子
  14. int num = rand()% + ;
  15. int value;
  16. for(;;)
  17. {
  18. printf("猜猜看我年龄多大(请输入一个整数)>>>: ");
  19. scanf("%d",&value);
  20. if(value > num)
  21. {
  22. printf("我有那么老吗?\n");
  23. }
  24. else if(value < num)
  25. {
  26. printf("我看起来这么小吗?\n");
  27. }
  28. else
  29. {
  30. printf("太棒了,你猜对啦~\n");
  31. break;
  32. }
  33. }
  34. return ;
  35. }
  36. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  37. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o guess_age guess_age.c
  38. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./guess_age
  40. 猜猜看我年龄多大(请输入一个整数)>>>:
  41. 我看起来这么小吗?
  42. 猜猜看我年龄多大(请输入一个整数)>>>:
  43. 我有那么老吗?
  44. 猜猜看我年龄多大(请输入一个整数)>>>:
  45. 我有那么老吗?
  46. 猜猜看我年龄多大(请输入一个整数)>>>:
  47. 我看起来这么小吗?
  48. 猜猜看我年龄多大(请输入一个整数)>>>:
  49. 我有那么老吗?
  50. 猜猜看我年龄多大(请输入一个整数)>>>:
  51. 我看起来这么小吗?
  52. 猜猜看我年龄多大(请输入一个整数)>>>:
  53. 太棒了,你猜对啦~
  54. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  55. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

 6>.循环语句练习二(打印等腰三角形)

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat isosceles_triangle.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. int row;
  13. printf("请输入要打印等腰三角形的行数:>>> ");
  14. scanf("%d",&row);
  15. for (int i = ;i <= row;i++)
  16. {
  17. for (int j =;j <= row - i;j++)
  18. {
  19. printf(" ");
  20. }
  21.  
  22. for (int k = ;k <= i * -;k++)
  23. {
  24. printf("*");
  25. }
  26. printf("\n");
  27. }
  28. return ;
  29. }
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  31. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o isosceles_triangle isosceles_triangle.c -std=c99
  33. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  34. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./isosceles_triangle
  35. 请输入要打印等腰三角形的行数:>>>
  36. *
  37. ***
  38. *****
  39. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  40. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./isosceles_triangle
  41. 请输入要打印等腰三角形的行数:>>>
  42. *
  43. ***
  44. *****
  45. *******
  46. *********
  47. ***********
  48. *************
  49. ***************
  50. *****************
  51. *******************
  52. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  53. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

四.跳转语句break和contiune语句

1>.break语句

  1.   在switch条件语句和循环语句中都可以使用break语句:
        当它出现在switch条件语句时,作用是终止某个case并跳出switch结构。
        当它出现在循环语句中,作用是跳出当前内循环语句,执行后面的代码。
        当它出现嵌套循环语句中,跳出最近的内层循环语句,执行后面的代码。
  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat do_while.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int a = ;
  16. do
  17. {
  18. a++;
  19. if (a == )
  20. {
  21. break;
  22. }
  23. }while(a); //需要注意的是,尽管没有上面的if条件判断语句,该循环并非死循环,只是执行的次数较多而已,因为a是一个有符号int类型的数字,而int类型是有上限的
  24.  
  25. printf("%d\n",a);
  26.  
  27. return EXIT_SUCCESS;
  28. }
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o do_while do_while.c
  31. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  32. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./do_while
  33.  
  34. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat do_while.c

2>.continue语句

  1.   在循环语句中,如果希望立即终止本次循环,并执行下一次循环,此时就需要使用continue语句。
  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat continue_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main(void)
  14. {
  15. int index = ;
  16. while (index < )
  17. {
  18. index++;
  19. if(index % == || index % == || index / == ) //过滤掉带7和7的倍数的数字
  20. {
  21. continue;
  22. }
  23. printf("数字:%d\n",index);
  24. }
  25.  
  26. return EXIT_SUCCESS;
  27. }
  28. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  29. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o continue_demo continue_demo.c
  30. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  31. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./continue_demo
  32. 数字:
  33. 数字:
  34. 数字:
  35. 数字:
  36. 数字:
  37. 数字:
  38. 数字:
  39. 数字:
  40. 数字:
  41. 数字:
  42. 数字:
  43. 数字:
  44. 数字:
  45. 数字:
  46. 数字:
  47. 数字:
  48. 数字:
  49. 数字:
  50. 数字:
  51. 数字:
  52. 数字:
  53. 数字:
  54. 数字:
  55. 数字:
  56. 数字:
  57. 数字:
  58. 数字:
  59. 数字:
  60. 数字:
  61. 数字:
  62. 数字:
  63. 数字:
  64. 数字:
  65. 数字:
  66. 数字:
  67. 数字:
  68. 数字:
  69. 数字:
  70. 数字:
  71. 数字:
  72. 数字:
  73. 数字:
  74. 数字:
  75. 数字:
  76. 数字:
  77. 数字:
  78. 数字:
  79. 数字:
  80. 数字:
  81. 数字:
  82. 数字:
  83. 数字:
  84. 数字:
  85. 数字:
  86. 数字:
  87. 数字:
  88. 数字:
  89. 数字:
  90. 数字:
  91. 数字:
  92. 数字:
  93. 数字:
  94. 数字:
  95. 数字:
  96. 数字:
  97. 数字:
  98. 数字:
  99. 数字:
  100. 数字:
  101. 数字:
  102. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat continue_demo.c #过滤掉带7和7的倍数的数字

 3>.goto语句(无条件跳转,尽量少用)

  1. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat goto_demo.c
  2. /*
  3. @author :yinzhengjie
  4. blog:http://www.cnblogs.com/yinzhengjie
  5. EMAIL:y1053419035@qq.com
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(void)
  11. {
  12. goto END; //无条件跳转到END的标识
  13. printf("第一行字母:a\n");
  14.  
  15. END: //我们定义的END标识,需要注意的是,这里是冒号(":"),而非分号(“;”)哟~
  16. printf("第二行字母:A\n");
  17.  
  18. return ;
  19. }
  20. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  21. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o goto_demo goto_demo.c
  22. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  23. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  24. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./goto_demo
  25. 第二行字母:A
  26. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
  27. [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#

C语言基础知识-程序流程结构的更多相关文章

  1. Golang 入门系列(三)Go语言基础知识汇总

    前面已经了 Go 环境的配置和初学Go时,容易遇到的坑,大家可以请查看前面的文章 https://www.cnblogs.com/zhangweizhong/category/1275863.html ...

  2. C语言基础知识-数组和字符串

    C语言基础知识-数组和字符串 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数组概述 在程序设计中,为了方便处理数据把具有相同类型的若干变量按有序形式组织起来的方式我们称为数组 ...

  3. PHP丨PHP基础知识之流程控制WHILE循环「理论篇」

    昨天讲完FOR循环今天来讲讲他的兄弟WHILE循环!进入正题: while是计算机的一种基本循环模式.当满足条件时进入循环,进入循环后,当条件不满足时,跳出循环.while语句的一般表达式为:whil ...

  4. C语言基础知识-数据类型

    C语言基础知识-数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常量与变量 1>.关键字 C的关键字共有32个. >.数据类型关键字(12个) char,s ...

  5. OC语言基础知识

    OC语言基础知识 一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能 ...

  6. 李洪强iOS开发之OC语言基础知识

    OC语言基础知识 一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能 ...

  7. ios开发学习笔记001-C语言基础知识

    先来学习一下C语言基础知识,总结如下: 在xcode下编写代码. 1.编写代码 2.编译:cc –c 文件名.c 编译成功会生成一个 .o的目标文件 3.链接:把目标文件.o和系统自带的库合并在一起, ...

  8. C语言基础知识-运算符与表达式

    C语言基础知识-运算符与表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用运算符分类 1>.算术运算符 用于处理四则运算. 2>.赋值运算符 用于将表达式的 ...

  9. PHP语言基础知识

    目录 前言 第一章 PHP语言学习介绍 1.1 PHP部署安装环境 1.2 PHP代码工具选择 第二章 PHP代码基本语法 2.1 PHP函数知识介绍 2.2 PHP常量变量介绍 2.2.1 PHP变 ...

随机推荐

  1. Hadoop深入学习之HA

    1. 基本原理 2.x版本中,HDFS架构解决了单点故障问题,即引入双NameNode架构,同时借助共享存储系统来进行元数据的同步,共享存储系统类型一般有几类,如:Shared NAS+NFS.Boo ...

  2. 转载:postman自动设置token(csrf及authorization token)

    原文链接: https://knktc.com/2018/06/03/postman-set-token/ 正文引用: 近期在开发一个Django的项目,由于开启了csrf防护,所以需要在请求的hea ...

  3. golang xorm时区问题

    mysql连接后面加 &loc=Local 否则执行sql的时间格式,存到数据库会按0时区 UTC存储

  4. vs2017 vs2019 打开cs文件提示无法识别的GUID格式

    总结一句话 no zuo no die. 是我自己在注册表中给vs增加了自动以管理员身份运行,把值给错了,弄成了 ~ RUNASADMIN WIN7RTM, 改成 ~ RUNASADMIN 后OK.还 ...

  5. JS增删改查localStorage实现搜索历史功能

    <script type="text/javascript"> var referrerPath = "@ViewBag.ReferrerPath" ...

  6. [ARM-LInux开发]linux设备驱动makefile入门解析

    以下内容仅作参考,能力有限,如有错误还请纠正.对于一个普通的linux设备驱动模块,以下是一个经典的makefile代码,使用下面这个makefile可以完成大部分驱动的编译,使用时只需要修改一下要编 ...

  7. 前端与算法 leetcode 27.移除元素

    目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...

  8. 测试标题CSS样式

    标题1 内容1 标题2 内容2 标题3 内容3

  9. idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder”

    <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artif ...

  10. [转帖]Java中重写和重载与多态的关系

    Java中重写和重载与多态的关系 2019-09-05 00:57:41 留下一天今天 阅读数 67  收藏 更多 分类专栏: java进阶之路   版权声明:本文为博主原创文章,遵循CC 4.0 B ...