当前位置:网站首页>MOOC翁恺C语言 第四周:进一步的判断与循环:3.多路分支4.循环的例子5.判断和循环常见的错误
MOOC翁恺C语言 第四周:进一步的判断与循环:3.多路分支4.循环的例子5.判断和循环常见的错误
2022-07-28 05:28:00 【W.934】
一、多路分支
switch-case
switch(控制变量式){
case 常量:
语句
......
case 常量:
语句
......
default:
语句
......
}
控制表达式只能是整数型的结果。(表达式只能是int类型的)
常量可以是常数,也可以是常数计算的表达式。(C99可用)
break
case不能阻止句子继续往下进行,而break可以。
switch语句可以看做是一种基于计算的跳转,计算控制表达式的值后,程序会跳转到相匹配的case(分支标号)处。分支标号只是说明switch内部位置的路标,在执行完分支中的最后一条语句后,如果后面没有break,就会顺序执行到下面的case里去,直到遇到一个break,或者switch结束为止。
二、循环的例子
1.循环计算
小套路:计算之前先保存原始的值,后面可能有用。
2.计数循环
小套路:如果要模拟运行一个很大次数的循环,可以模拟较少的循环次数,然后做出推断。
3.算平均数

4. 猜数游戏
随机数:rand
每次召唤rand()就得到一个随机的整数。
%100:x%n的结果是[0,n-1]的一个整数
5.整数的分解
一个整数是由1至多位数字组成的,如何分解出整数的各个位上的数字,然后加以计算。
对一个整数做%10的操作,就得到它的个位数;
对一个整数做/10的操作,就去掉了它的个位数;
然后再对②的结果做%10,就得到原来的十位数了;
以此类推......
三、判断和循环常见的错误
1.if语句常见的错误
1)忘了大括号 //永远在if和else后面加上{},即使当时后面只有一条语句。
2)if后面的分号 //if语句后面不加分号;
3)错误使用==和= //if只要求()里的值是零或非零(即是或否)//=是赋值//注意warning提示
4)else的使用
代码风格:
在if和else之后必须加上大括号形成语句块;大括号内的语句缩进一个tab的位置。
边栏推荐
- [learning notes] linked list operation
- Cocos2d-x learning notes Tile Map tiledmap
- 单项链表的创建、遍历以及按要求查找结点
- DNS domain name resolution service
- Array to linked list
- OSI seven layer model
- Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
- SSH服务配置
- Which is the best and most cost-effective air conduction headset recommended
- Wechat applet custom compilation mode
猜你喜欢

How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with

组管理和权限管理

技术分享 | 如何模拟真实使用场景?mock 技术来帮你

raid磁盘阵列

1、 PXE overview and installation

Applets: WSX scripts

Si Han talks about the professional development of testers

Shell script - regular expression

shell脚本——“三剑客”之awk命令

Technology sharing | detailed explanation of actual combat interface test request methods get, post
随机推荐
Which brand of air conduction earphones is good and highly praised
单元测试框架Jest搭配TypeScript的安装与配置
Network - transport layer (detailed version)
Hdu-5806-nanoapelovesequence Ⅱ (ruler method)
Hdu-5805-nanoape loves sequence (thinking questions)
PKU-2524-Ubiquitous Religions(并查集模板)
HDU-2036-改革春风吹满地(多边形面积模板)
Technology sharing | detailed explanation of actual combat interface test request methods get, post
Custom components -- slots
JSON notes
Hdu-2036-reform spring breeze blowing all over the ground (polygon area template)
SSH service configuration
Esxi community network card driver
OSI seven layer model
Custom component -- communication between parent and child components
RAID disk array
Network - network layer
What is the most practical gift for Tanabata? A gift that will never go wrong is worth buying
技术分享 | 实战详解接口测试请求方式Get、post
It is recommended to wear air conduction earphones, which do not need to wear in ear