当前位置:网站首页>分支语句和循环语句
分支语句和循环语句
2022-07-06 09:19:00 【犇犇犇犇犇犇】
分支语句
if - else
int main()
{
if(condition)//表达式
{
//语句
}
int n = 3;
if (n = 5)// = 是赋值运算符 等价于if(5)非零就是true执行判断
printf("hehe\n");//会打印hehe
if (3 == n)//为了防止你出现少打一个=的情况你可以把常量放在前面
//这时如果你少打了一个=就会报错
printf("hehe\n");
int age = 10;
if (18 < age <= 28)//这里的意思是先判断age是否大于18,否,返回0,0<=28
//true 返回1 所以它会打印成年
//如果我们想写出数学中这样的逻辑应该用 && 逻辑运算符
printf("成年\n");
if (age > 18 && age <=28)
printf("成年\n");
return 0;
}
int main()
{
//if else 不加花括号只能控制下面一行语句
//控制多行语句时要加入花括号
//并且 else只和最近的if匹配
//加上花括号 可以让你的逻辑更清晰避免出现
//缩进未对齐的逻辑错乱等
//所以请养成优秀的代码风格,加上花括号
//int age = 10;
//if (age < 18)
// printf("未成年\n");
// printf("不能喝酒\n");
//else
// printf("成年\n");
//if (age < 18)
// printf("未成年\n");
// if (age > 3)
// printf("age=10");
//else
// printf("age !=10");
int age = 10;
if (age < 18)
{
printf("未成年\n");
printf("不能喝酒\n");
}
else
{
printf("成年\n");
}
if (age < 18)
{
printf("未成年\n");
}
if (age > 3)
{
printf("age=10");
}
else
{
printf("age !=10");
}
return 0;
}
switch
int main()
{
//switch case 语句
//switch(c) c不可以是float类型
//case - 只能是整形常量表达式 可以是字符因为字符存储的是字符的ASCII码
//进入某个case后执行完所以语句后用break结束switch
//不然他会按顺序依次执行其它case直到switch结束
//当输入的值没有case匹配时,用default来接收所有其它的值
//当多个输入是一个结果时请下面看代码书写
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;*/
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
default:
printf("选择错误\n");
break;
}
return 0;
}
请保持良好的编程习惯,在最后一个case或者default中都加上break
循环结构
while
int main()
{
//打印1-100的奇数
//两种思路都可以
int i = 1;
while (i < 100)
{
//if (i % 2 == 1)
//{
// printf("%d ", i);
//}
//i++;
printf("%d ", i);
i += 2;
}
return 0;
}
int main()
{
//这里写一下知识点
//其实当我们键盘输入什么时(比如asdadf\n)回车结束输入 --
//会先存到一个输入缓存区(asdadf) --
//这是当我们使用scanf getchar拿数据时就是在缓存区拿走数据
//scanf会在遇到空格,回车时结束拿数据 getchar只拿一个数据无论什么
//EOF - end of file 文件末尾的结束标志
//举一个例子
//假设密码是一个字符串
char password[20] = {
0 };
printf("请输入密码>");
scanf("%s", password);//asde回车 asaew asdq
//getchar();//拿走\n这时缓存区才为空,才能让你选择Y/N
//当你的缓存区还有很多字符时,所以你就需要清空缓存区
int ch = 0;
while ( (ch = getchar()) != '\n')//getchar的返回类型为int
{
;
}
printf("请确认密码(Y/N):>");//直接输出NO 不让你选择
int ret = getchar();
if (ret == 'Y')
{
printf("Yes\n");
}
else
{
printf("NO\n");
}
//int ch = 0;
//while ( (ch = getchar()) != EOF)
//{
// putchar(ch);
//}
return 0;
}
for
int main()
{
//我们先来看while
int i = 0;//初始化
while ( i < 10)//判断
{
printf("%d ", i);
i++;//调整
}
return 0;
}
int main()
{
//for循环是平常用的最多的循环
int i = 0;
for (i = 0; i < 10; i++)//for循环把 初始化,判断,调整放到了一起
//所以它更容易理解
{
printf("%d ", i);
}
//我们平常编程就是 办法+写代码
//办法就是编程思维 写代码就是用计算机语言来实现思维
//所以思维的培养很重要
return 0;
}
int main()
{
//关于写for循环的建议
//1.不要在for循环体内修改循环变量,防止for循环失去控制
//2.建议for循环控制变量的取值是采用前闭后开区间的写法(当然特殊情况除外)
int i = 0;
//for(i = 0; i <= 9 ;i++) [0,9]
for (i = 0; i < 10; i++) // [0,10) 这样更容易理解你在干嘛
{
printf("%d ", i);
// i = 12;
}
return 0;
}
int main()
{
//for循环的判断部分省略就等于条件恒成立
for (;;)
{
printf("hehe\n");
}
return 0;
}
//求1*2*3...*n的阶乘
//1!+2!+3!...+n!
int main()
{
int i = 0;
int n = 0;
int ret = 1;
// 当求到20左右的阶乘时,就会出现溢出现象,
//这是超出了int型值的范围,这里不讨论溢出问题
//对于大数的实现需要我们重新写一个乘法函数进行处理
int sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
sum += ret; // 阶乘的和
}
printf("%d\n", ret);
printf("%d\n", sum);
return 0;
}
do while
int main()
{
//do while 就是无论什么直接循环一次然后再判断
//上来就是干!
int i = 0; //初始化
do
{
printf("%d ", i);
i++;//调整
}
while ( i < 10);//判断
return 0;
}
int main()
{
//这里在讲一下各个循环的break和continue情况
int i = 0;
//while (i < 10)
//{
// if (i == 5)
// //break;//打印0-4
// continue;//打印0-4后死循环
// printf("%d ", i);
// i++;
//}
//for (i = 0; i < 10; i++)
//{
// if (i == 5)
// //break;//打印0-4
// continue;//打印0-4 6-9
// printf("%d ", i);
//}
do
{
if (i == 5)
//break;//打印0-4
continue;//打印0-4后死循环
printf("%d ", i);
i++;
} while (i < 10);
//其实这是因为while,do while的调整在continue后面,
//continue的作用就是跳出这一次循环,不执行后面的语句直接进入下一次循环
//所以i一直就等于 5
return 0;
}
补充
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
//for循环每次执行完循环体就调整然后再判断
//而初始化就执行一次
printf("%d ", i);
}
return 0;
}
边栏推荐
- [algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
- Record: Navicat premium can't connect to MySQL for the first time
- NovAtel 板卡OEM617D配置步骤记录
- 闇の連鎖(LCA+树上差分)
- 国企秋招经验总结
- How to improve the deletion speed of sequential class containers?
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- 121道分布式面试题和答案
- isEmpty 和 isBlank 的用法区别
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
猜你喜欢
Fundamentals of UD decomposition of KF UD decomposition [1]
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Iterable、Collection、List 的常见方法签名以及含义
How to ensure data consistency between MySQL and redis?
Wechat applet development experience
Fairygui bar subfamily (scroll bar, slider, progress bar)
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
FairyGUI循環列錶
地球围绕太阳转
音乐播放(Toggle && PlayerPrefs)
随机推荐
【干货】提升RTK模糊度固定率的建议之周跳探测
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
Chromatic judgement bipartite graph
FairyGUI循環列錶
[GNSS data processing] Helmert variance component estimation analysis and code implementation
阿里云微服务(四) Service Mesh综述以及实例Istio
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
KF UD分解之UD分解基础篇【1】
Record: solution of 404 error of servlet accessing database in dynamic web project
Basic DOS commands
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Matlab读取GNSS 观测值o文件代码示例
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
The earth revolves around the sun
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
平衡二叉树详解 通俗易懂
使用rtknavi进行RT-PPP测试
wsl常用命令