当前位置: 首页 > news >正文

C语言深度解剖-关键字(7)

目录

switch case 语句

理解:

补充:

深入理解:

default 语句:

case语句:

总结:

do、while、for 关键字

while

for

do while

各种死循环方法:

while

for

do while

getchar

写在最后:


switch case 语句

理解:

例:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int day = 0;//任何具有判定能力的语法结构,都必须具备:判定+分支printf("please choose your day:>");scanf("%d", &day);switch (day)//整形或者整形表达式{case 1://case 用来进行判定功能printf("星期一\n");break;//break 用来进行分支功能case 2:printf("星期二\n");break;	case 3:printf("星期三\n");break;	case 4:printf("星期四\n");break;	case 5:printf("星期五\n");break;	case 6:printf("星期六\n");break;case 7:printf("星期日\n");break;default:printf("输入错误\n");}return 0;
}

这样我们输入几,输出的就是星期几。

如果:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int day = 0;//任何具有判定能力的语法结构,都必须具备:判定+分支printf("please choose your day:>");scanf("%d", &day);switch (day)//整形或者整形表达式{case 1://case 用来进行判定功能printf("星期一\n");//break;//break 用来进行分支功能case 2:printf("星期二\n");//break;	case 3:printf("星期三\n");//break;	case 4:printf("星期四\n");//break;	case 5:printf("星期五\n");break;	case 6:printf("星期六\n");break;case 7:printf("星期日\n");break;default:printf("输入错误\n");}return 0;
}

输入:

输入:1

输出:

输出:
please choose your day:>1
星期一
星期二
星期三
星期四
星期五

补充:

写这个语句时一定要添加上default 语句,

非常重要,哪怕用不上:

例:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int day = 0;//任何具有判定能力的语法结构,都必须具备:判定+分支printf("please choose your day:>");scanf("%d", &day);switch (day)//整形或者整形表达式{case 1://case 用来进行判定功能printf("星期一\n");break;//break 用来进行分支功能case 2:printf("星期二\n");break;	case 3:printf("星期三\n");break;	case 4:printf("星期四\n");break;	case 5:printf("星期五\n");break;	case 6:printf("星期六\n");break;case 7:printf("星期日\n");break;default:printf("输入错误\n");}return 0;
}

输入:

输入:8

输出:

输出:
please choose your day:>8
输入错误

深入理解:

建议不要用:

例:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int day = 0;//任何具有判定能力的语法结构,都必须具备:判定+分支printf("please choose your day:>");scanf("%d", &day);switch (day)//整形或者整形表达式{case 1://case 用来进行判定功能printf("星期一\n");break;//break 用来进行分支功能case 2:{//如果想执行多条语句,要加{ }printf("星期二\n");printf("星期三\n");printf("星期四\n");printf("星期五\n");}break;	case 6:printf("星期六\n");break;case 7:printf("星期日\n");break;default:printf("输入错误\n");}return 0;
}

输入:

输入:2

输出:

输出:
please choose your day:>2
星期二
星期三
星期四
星期五

那如果想要多种情况执行同一条语句呢?

推荐:

例:


#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int day = 0;//任何具有判定能力的语法结构,都必须具备:判定+分支printf("please choose your day:>");scanf("%d", &day);switch (day)//整形或者整形表达式{case 1:case 2:case 3:case 4:case 5:printf("周内\n");break;case 6:printf("星期六\n");break;case 7:printf("星期日\n");break;default:printf("输入错误\n");}return 0;
}

这样无论你输入1 2 3 4 5都会显示周内。

default 语句:

default 它一定要写在最后吗?

不,它放到哪里都可以,

只是我们更习惯放在最后,这样更符合语义。

case语句:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{const int a = 4;switch (a){case 4:printf("is ok\n");}return 0;
}

因为用const 修饰后是常量,所以这段代码时编的过去的,

但是:

这种就不行,

一定要注意:

总结:

do、while、for 关键字

while

例:

#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int main()
{int count = 10;//循环条件初始化while (count > 0)//循环条件判定{printf("count = %d\n", count);count--;//循环条件更新}return 0;
}

for

例:

(比较推荐)一目了然

#include <stdio.h>int main()
{for (int i = 0; i < 10; i++){printf("i = %d\n", i);}return 0;
}

do while

例:

#include <stdio.h>int main()
{int count = 10;//循环条件初始化do{printf("count = %d\n", count);count--;//循环条件更新} while (count > 10);//循环条件判断return 0;
}

各种死循环方法:

while

#include <stdio.h>int main()
{while(1){printf(".");}return 0;
}

for

#include <stdio.h>int main()
{while (1){int c = getchar();if (c == '#'){break;//结束循环}printf("%c\n", c);}printf("while end...\n");return 0;
}

do while

#include <stdio.h>int main()
{do{printf(".");} while (1);return 0;
}

getchar

例:

#include <stdio.h>int main()
{while (1){int c = getchar();if (c == '#'){break;//结束循环}printf("%c\n", c);}printf("while end...\n");return 0;
}

输出:

 我们发现有点奇怪,

我们只进行了一次换行,但是却换了两行,

因为我们输入字符的时候,按了一下回车,那个回车也被getchar接受了。

写在最后:

以上就是本篇文章的内容了,感谢你的阅读。

如果喜欢本文的话,欢迎点赞和评论,写下你的见解。

如果想和我一起学习编程,不妨点个关注,我们一起学习,一同成长。

之后我还会输出更多高质量内容,欢迎收看。

http://www.lryc.cn/news/8893.html

相关文章:

  • 利用JavaScript编写Python内置函数查询工具
  • 【MySQL进阶】SQL优化
  • 最新版海豚调度dolphinscheduler-3.1.3配置windows本地开发环境
  • csv文件完整操作总结
  • 时间序列预测--基于CNN的股价预测(Matlab代码实现)
  • Dubbo与Spring Cloud优缺点分析(文档学习个人理解)
  • 单元测试工具——JUnit的使用
  • Linux_基本权限
  • 3、JavaScript面试题
  • YUV图像
  • .net6API使用AutoMapper和DTO
  • IO知识整理
  • 【正点原子FPGA连载】第十三章QSPI Flash读写测试实验 摘自【正点原子】DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南
  • 深入理解mysql的内核查询成本计算
  • LeetCode 141. 环形链表
  • git提交
  • Java中常见的编码集问题
  • 数据结构与算法(Java版) | 就让我们来看看几个实际编程中遇到的问题吧!
  • 【C++算法】dfs深度优先搜索(上) ——【全面深度剖析+经典例题展示】
  • 总结高频率Vue面试题
  • IP协议详解
  • webpack5 基础配置
  • IDEA入门安装使用教程
  • Lambda表达式使用及详解
  • JAVA练习52-打家劫舍
  • 简单谈一谈幂等测试
  • typescript复习笔记
  • webstorm开发electron,调试主进程方案
  • 2W字正则表达式基础知识总结,这一篇就够了!!(含前端常用案例,建议收藏)
  • 自学web前端觉得好难,可能你遇到了这些困境