当前位置:网站首页>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的位置。
边栏推荐
- 技术分享 | 使用 cURL 发送请求
- HDU-1159-CommonSubsequence(LCS最长公共子序列)
- Hdu-1097-a hard puzzle (fast power)
- Which is the best and most cost-effective air conduction headset recommended
- Archery database audit platform deployment
- Ubuntu18.04 set up redis cluster [learning notes]
- Hdu-5783 divide the sequence (greedy water question)
- [learning notes] VIM editor
- DNS forward resolution experiment
- 技术分享 | 如何模拟真实使用场景?mock 技术来帮你
猜你喜欢

MySQL master master

LNMP搭建过程详解

Use powercli to create a custom esxi ISO image

How to describe a bug and the definition and life cycle of bug level

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

What's a good gift for Tanabata? Niche and advanced product gift recommendation

Esxi community network card driver

Which is the best and most cost-effective air conduction headset recommended

Technology sharing | how to simulate real use scenarios? Mock technology to help you

How to calculate the size of structure, segment and Consortium (common body)
随机推荐
Applets: lifecycle
技术分享 | 接口测试常用代理工具
组管理和权限管理
Common models in software development
Small tips
Tcp/ip five layer model
HDU-1097-A hard puzzle(快速幂)
Array to linked list
My deployment notes
CentOS7部署MySQL数据库服务器
Which brand of air conduction earphones is better? These four should not be missed
Network - transport layer (detailed version)
Shell script - regular expression
Which brand of air conduction earphones is good and highly praised
[learning notes] tool
FTP service
Applet creation component
Hdu-1159-commonsubsequence (LCS longest common subsequence)
Hdu-1097-a hard puzzle (fast power)
Escape character notes