if语句

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. const int FREEZING = 0;
  5. float temperature;
  6. int cold_days = 0;
  7. int all_days = 0;
  8. printf("Enter the list of daily low temperatures.\n");
  9. printf("Use Celsius,and enter q to quit.\n");
  10. while (scanf_s("%f", &temperature)) {
  11. all_days++;
  12. if (temperature < FREEZING)
  13. cold_days++;
  14. }
  15. if (all_days != 0)
  16. printf("%d days total:%.lf%% were below freezing.\n", all_days,
  17. 100.0*(float)cold_days / all_days);
  18. if (all_days == 0)
  19. printf("No data entered!\n");
  20. system("pause");
  21. return 0;
  22. }
  23. /*
  24. if (expression) 分支语句。如果expression为真,则执行statement
  25. statement
  26. */

if else语句

  1. /*取自上个例子*/
  2. if (all_days != 0)
  3. printf("%d days total:%.lf%% were below freezing.\n", all_days,
  4. 100.0*(float)cold_days / all_days);
  5. else
  6. printf("No data entered!\n");
  7. /*
  8. if (expression) 如果条件为真,执行statement1;如果条件为假,则执行statement2
  9. statement1
  10. else
  11. statement2
  12. */
getchar()和putchar()

两者只处理字符

  1. #include<stdio.h>
  2. #define SPACE ' '
  3. int main(void)
  4. {
  5. char ch;
  6. ch = getchar();
  7. /*该函数不带任何其他参数,它从输入队列中返回一个字符*/
  8. while (ch != '\n')
  9. {
  10. if (ch == SPACE)
  11. putchar(ch); //打印字符
  12. else
  13. putchar(ch + 1);
  14. ch = getchar();
  15. }
  16. putchar(ch);
  17. system("pause");
  18. return 0;
  19. }
ctype.h头文件
函数名 含义判断
isalnum() 字母或数字
isalpha() 字母
isblank() 标准的本地化空白字符
iscntrl() 控制字符
isdigit() 数字
isgraph() 除空格之外的任意可打印字符
islower() 小写字母
isprint() 可打印字符
ispunct() 除空格或字母数字字符以外的任何可打印字符
isspace() 空白字符
isupper() 大写字母
isxdigit() 十六进数字符
tolower() 返回小写字符
toupper() 返回大写字符
多重选择else if
  1. #include<stdio.h>
  2. #define RATE1 0.13230
  3. #define RATE2 0.15040
  4. #define RATE3 0.30025
  5. #define RATE4 0.34025
  6. #define BREAK1 360.0
  7. #define BREAK2 468.0
  8. #define BREAK3 720.0
  9. #define BASE1 (RATE1*BREAK1)
  10. #define BASE2 (BASE1+(RATE2*(BREAK2-BREAK1)))
  11. #define BASE3 (BASE1+BASE2+(RATE3*(BREAK3-BREAK2)))
  12. int main(void)
  13. {
  14. double kwh;
  15. double bill;
  16. printf("Please enter the kwh used.\n");
  17. scanf_s("%lf", &kwh);
  18. if (kwh <= BREAK1)
  19. bill = RATE1 * kwh;
  20. else if (kwh <= BREAK2)
  21. bill = BASE1 + (RATE2*(kwh - BREAK1));
  22. else if (kwh <= BREAK3)
  23. bill = BASE2 + (RATE3*(kwh - BREAK2));
  24. else
  25. bill = BASE3 + (RATE4*(kwh - BREAK3));
  26. printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill);
  27. system("pause");
  28. return 0;
  29. }
else与if配对

如果没有花括号,else与离他最近的if匹配,除非最近的if被花括号括起来了。

逻辑运算符

逻辑运算符 含义
&&
||
备用拼写:ios646.h头文件
逻辑运算符 替代
&& and
|| or
! not
优先级

!运算符只比圆括号优先级低,&&运算符比||高,但是二者的优先级都比关系运算符低,比赋值运算符高。

求值顺序

求值顺序是从左到右,程序在从一个运算对象执行到下一个运算对象之前,所有副作用都会生效。而且,C保障一旦发现某个元素让整个表达式无效,便立即停止求值。

范围
  1. if (range >=90&&range<=100)

统计单词的程序

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<stdbool.h>
  4. #define STOP '|'
  5. int main(void)
  6. {
  7. char c;
  8. char prev;
  9. long n_chars = 0L;
  10. int n_lines = 0;
  11. int n_words = 0;
  12. int p_lines = 0;
  13. bool inword = false;
  14. printf("Enter text to be analyzed(| to terminate):\n");
  15. prev = "\n";
  16. while ((c = getchar()) != STOP) {
  17. n_chars++;
  18. if (c == '\n') {
  19. n_lines++;
  20. }
  21. if (!isspace(c) && !inword) {
  22. inword = true;
  23. n_words++;
  24. }
  25. if (isspace(c) && inword) {
  26. inword = false;
  27. }
  28. prev = c;
  29. }
  30. if (prev != '\n') {
  31. p_lines = 1;
  32. }
  33. printf("characters = %ld,words = %d,lines=%d,", n_chars, n_words, n_lines);
  34. printf("partial lines = %d\n", p_lines);
  35. system("pause");
  36. return 0;
  37. }

条件运算符:?:

  1. expression1?expression2:expression3
  2. /*
  3. 如果expression1为真,那么整个条件表达式的值与expression2的值相同;
  4. 如果expression1为假,那么整个条件表达式的值与expression3的值相同;
  5. */
  6. max = (a>b)?a:b;

循环辅助:continue和break

continue语句
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. const float MIN = 0.0f;
  5. const float MAX = 100.0f;
  6. float score;
  7. float total = 0.0f;
  8. int n = 0;
  9. float min = MAX;
  10. float max = MIN;
  11. printf("Enter the first score(q to qiut):");
  12. while (scanf_s("%f",&score)==1)
  13. {
  14. if (score<MIN||score>MAX)
  15. {
  16. printf("%0.1f is an invalid value.Try again:", score);
  17. continue; //继续执行while
  18. }
  19. printf("Accepting %0.1f:\n", score);
  20. min = (score < min) ? score : min;
  21. max = (score > max) ? score : max;
  22. total += score;
  23. n++;
  24. printf("Enter next score(q to qiut):");
  25. }
  26. if (n > 0)
  27. {
  28. printf("Average of %d scores is %0.1f.\n", n, total);
  29. printf("Low = %0.1f,high=%0.1f\n", min, max);
  30. }
  31. else
  32. printf("No valid scores were entered.\n");
  33. system("pause");
  34. return 0;
  35. }
break
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. float length, width;
  5. printf("Enter the length of the rectangle:\n");
  6. while ((scanf_s("%f", &length)) == 1)
  7. {
  8. printf("Length = %0.2f\n", length);
  9. printf("Enter its width:\n");
  10. if ((scanf_s("%f", &width) != 1))
  11. break; //使得跳出这个循环
  12. printf("Width = %0.2f", width);
  13. printf("The area of the rectangle is %f", length*width);
  14. printf("Enter the length of the rectangle:\n");
  15. }
  16. printf("Done!\n");
  17. system("pause");
  18. return 0;
  19. }

多重选择:switch和break

  1. switch(expression) //expression只能是一个值,而不能是范围
  2. {
  3. case expression_1: //只会读取首字母
  4. statement1;
  5. break;
  6. case expression_2:
  7. statement2;
  8. break;
  9. case expression_3:
  10. statement3;
  11. break;
  12. ......
  13. default:expression_end;//如果没有对应的case,则跳转到default来
  14. }
多重标签:
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. char ch;
  5. int a_ct, e_ct, i_ct, o_ct, u_ct;
  6. a_ct = e_ct = i_ct = o_ct = u_ct = 0;
  7. printf("Enter some text;enter # to quit.\n");
  8. while ((ch=getchar())!='#')
  9. {
  10. switch (ch)
  11. {
  12. case 'a':
  13. case 'A':a_ct++;
  14. break;
  15. case 'e':
  16. case 'E':e_ct++;
  17. break;
  18. case 'i':
  19. case 'I':i_ct++;
  20. break;
  21. case 'o':
  22. case 'O':o_ct++;
  23. break;
  24. case 'u':
  25. case 'U':u_ct++;
  26. break;
  27. default:break;
  28. }
  29. }
  30. printf("number of vowels:A:%4d E:%4d I:%4d O:%4d U:%4d\n",a_ct,e_ct,i_ct,o_ct,u_ct);
  31. system("pause");
  32. return 0;
  33. }

goto语句

  1. goto part;//跳转到part去
  2. part:statement;//必须有一个标签

C语言之控制语言:分支和跳转的更多相关文章

  1. 第7章,c语言控制语句:分支和跳转

    7.1 if语句 通用形式:if(expression) statment 7.2 if else语句 通用形式:if(expression) startment else startment2 7. ...

  2. C控制语句--分支和跳转

    /*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...

  3. 【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转

    学习总结 1.if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else. 2.根据个人几年的编程经验,太多的if…else…嵌套会加大代码的 ...

  4. C 语言 - 分支、跳转和循环语句

    if 条件判断语句 if 语句结构 格式: if (表达式) { 语句; } 如果表达式成立,就执行大括号中的语句:否则跳过该 if 语句 #include <stdio.h> int m ...

  5. C Primer Plus学习笔记(六)- C 控制语句:分支和跳转

    if 语句: if 语句被称为分支语句(branching statement)或选择语句(selection statement) if 语句的通用形式: if (expression) state ...

  6. C语言讲义——结构化编程(分支、循环)

    顺序结构(从上到下) 分支结构(也叫选择结构) 循环结构 分支结构 if...else 最基本的分支结构是if(){}else{}. 为了代码的安全,同时也是出于代码规范的考虑,if()后面一定要加花 ...

  7. C语言进阶_分支语句

    勇气是在压力之下展现出的优雅. 一.简介 C语言提供了两种分支语句可供选用,一是if.......else....类型,一种是Switch语句.两种语句都能根据条件判断结果执行不同的指令,且能进行替换 ...

  8. [C语言入门笔记]分支结构与数组

    分支结构与数组 什么是分支结构? 分支结构是用户或者程序可以选择下一步执行哪个语句 分支结构有哪些? If If Else If Else If Switch 在初学者的学习过程中第一种和第二种比较普 ...

  9. C控制语句:分支和跳转

    小技巧:程序return前加个getchar();可以让程序停住.%%可以打印使printf()中打印出%号 #include<stdio.h>#define SPACE ''int ma ...

随机推荐

  1. 什么是平衡二叉树(AVL)

    前言 Wiki:在计算机科学中,AVL树是最早被发明的自平衡二叉查找树.在AVL树中,任一节点对应的两棵子树的最大高度差为1,因此它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度都 ...

  2. java_泛型2

    一.泛型_泛型概述及好处     1).在定义集合时,我们是希望集合中只存储一种类型的引用,这时可以使用泛型:       ArrayList<String> list = new Arr ...

  3. CentOS7 yum方式安装MySQL5.7

    转载至博客:https://www.cnblogs.com/bigbrotherer/p/7241845.html 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要 ...

  4. vue element-ui 2.3.4版本 input number值为0时 显示不出来

    解决:官方修复了这个bug.升级element-ui为2.3.5版本就好了

  5. 微信小程序--页面的生命周期和参数传递

    页面跳转: 1.函数 点击事件设置bindtap然后在本页面 js 设置函数 redirectTo:关闭当前页,跳转到指定页:   ( unload) navigateTo:保留当前页,跳转到指定页: ...

  6. 用 async/await 来处理异步

    昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了. 先说一下async的用法,它作为一个 ...

  7. Salesforce 小知识:大量“子记录”的处理方法

    大量"子记录"的存放 例子:系统中导入了很多"联系人"(Contact)记录,它们没有具体所属的"客户"(Account)记录.那么我们就要 ...

  8. 利用自定义View实现扫雷游戏

    游戏规则: 简单版的扫雷事实上就是一个9×9的矩阵,其中有十个点是雷,非雷方块的数字代表该方块周围八个方块中雷的个数.通过长按某一方块(方块会变红)认定该方块为玩家认为的雷,通过短按某一方块来“展开” ...

  9. Redis进阶学习笔记

    Redis是什么这里不用再说了吧?下面是官方的解释 Redis is an open source (BSD licensed), in-memory data structure store, us ...

  10. 通过Xtrabackup实现MySQL实例的全库备份与按需单库恢复

    在实际的生产环境中,为了管理方便,我们一般是通过 Xtrabackup实现实例的全库备份,即将实例上的所有数据库备份. 但是,考虑到快速恢复 我们常常面临的需求是快速还原单个数据库.针对初学者来说,网 ...