当前位置:网站首页>初识C语言~分支语句
初识C语言~分支语句
2022-04-22 07:48:00 【^O^玩转编程】
目录
前言
从今天开始,我将不间断的为大家分享我学C的历程,今天为大家分享的是分支语句。
C语言的语句
C语句可分为以下五类: 1. 表达式语句
2. 函数调用语句
3. 控制语句
4. 复合语句
5. 空语句
今天我要分享的是:控制语句
那么什么是控制语句呢?
简单来说便是控制程序执行流程的,在C语言中有三大家族。
今天先为大家介绍:爱选择的分支家族,后续将为大家介绍一根筋的循环家族和爱转弯的转向家族。
爱选择的分支家族
在这个家族中有二个成员,无所不能的大哥 if 和另辟蹊径的小弟switch。这个家族的成员有个共同的特点就是爱" 选择 ",来吧让我们一起走进这个有趣的家族吧!
无所不能的大哥 if
if大哥的认知:表达式(" 真 "就走起," 假 "就拜拜)
0表示假,非0表示真。
举例说明:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int main() { int a = 0; printf("选择学C语言的态度:\n"); scanf("%d", &a); if (0 != a) { printf("好好学C语言\n"); } else { printf("不好好学C语言\n"); } return 0; }打印:
认识if大哥的注意点:1 if 最爱离他最近的 else(相匹配)。
2 if后面执行多个语句要用{}。
另辟蹊径的小弟switch
对于switch语句我们要特别注意,他的判断条件为整形表达式。
举例说明:
#include<stdio.h> int main() { int day = 0; scanf("%d", &day); switch (day) { case 1: printf("星期一\n"); 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"); break; } return 0; }打印:
为改变需要
1. 输入1-5,输出的是“weekday”;
2. 输入6-7,输出“weekend”
我们要可以怎么写:
#include <stdio.h> //switch代码演示 int main() { int day = 0; scanf("%d", &day); switch (day) { case 1: case 2: case 3: case 4: case 5: printf("weekday\n"); break; case 6: case 7: printf("weekend\n"); break; } return 0; }switch的总结:1 是指跳出本次循环的意思break。
2 当switch表达式的值与所有的case标签的值都不匹配就会跳过所以语句。
3 default(/dɪ'fɔːlt/)可以出现在switch中的任何位置,且只能出现1条,作 用是当表示式中的值与csae不匹配时候就从default子句后面的语句就会执行。
4 我们一般在每个case语句后都加一个break养成编程好习惯。
版权声明
本文为[^O^玩转编程]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61552595/article/details/124274432
边栏推荐
- 解决办法Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Interview question 04.03 Specific depth node linked list (medium)
- WeChat official account - webpage authorization
- Mapbox sets the official map language to Chinese
- 安卓开发——SQLite和SQLiteDatabase应用实验6笔记
- Level 2: ACL access control list
- el-input输入框输入无效,且Error in data(): “ReferenceError: el is not defined“
- 电脑重置后恢复mysql服务
- Abbreviation for greater than / less than / equal to (abbreviation of SQL database includes mangodb)
- golang 环境搭建
猜你喜欢

重整笔记记录:【终极方法】在Vscode中用户创建自定义代码模板

Fabric测试示例,遇到orderer Exited(x) x seconds

Level 3: node status check, data view and update

Dynamic memory management of C

Level 2: ACL access control list

Level 3: node quota and other commands

HyperLedger Explorer 0.3.9环境搭建

Seven crimes of hackers in social engineering -- hooking

Pointer and string

Pointer and array (detailed operation)
随机推荐
编程中GMT和CST的含义
解决磁盘有空间但创建不了文件---修复服务器文件系统
Pointer and array (detailed operation)
ROM、RAM、SRAM、DRAM、Flash、SDRAM區別
236. The nearest common ancestor of a binary tree (medium)
[youqilin] version 22.04 lts will be released soon. The final notice is coming. Are you ready?
RHEL7 配置本地yum源
微信小程序手机号码如何进行解密
Constructor and toString
Lambda expression
require-dev.mikey179/vfsStream is invalid, it should not contain uppercase characters. Please use m
Matlab tip: to use 'xxx function', you must authorize, install and enable the following products: XXX toolbox
使用art-template继承时CompileError: Invalid or unexpected token generated
PCIe学习-PCIe总线体系结构入门:事务层-数据链路层-物理层(八)
Client server communication project 2
MFC demo of data encoding
聚看点 (详细版) 吃低保 每天几毛 青龙
617. Merge binary tree (easy)
SQL 语句中 “意想不到” 的操作
String replacement related topics (merging arrays)




