当前位置:网站首页>分支语句和循环语句
分支语句和循环语句
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;
}
边栏推荐
- 国企秋招经验总结
- How to ensure data consistency between MySQL and redis?
- 记录:下一不小心写了个递归
- 地球围绕太阳转
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- Record: Navicat premium can't connect to MySQL for the first time
- 记录:动态Web项目servlet访问数据库404错误之解决
- 【无标题】
- Compile GDAL source code with nmake (win10, vs2022)
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
猜你喜欢

几道高频的JVM面试题
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
![[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array

Edit distance (multi-source BFS)

国企秋招经验总结

Role movement in the first person perspective

Redis介绍与使用

The earth revolves around the sun

The port is occupied because the service is not shut down normally
随机推荐
Error: sorting and subscript out of bounds
一文搞定 UDP 和 TCP 高频面试题!
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
PRIDE-PPPAR源码解析
几道高频的JVM面试题
雇佣收银员【差分约束】
The earth revolves around the sun
系统设计学习(三)Design Amazon‘s sales rank by category feature
Usage differences between isempty and isblank
NovAtel 板卡OEM617D配置步骤记录
Record: solution of 404 error of servlet accessing database in dynamic web project
平衡二叉树详解 通俗易懂
【GNSS】抗差估计(稳健估计)原理及程序实现
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
阿里云微服务(三)Sentinel开源流控熔断降级组件
异常:IOException:Stream Closed
国企秋招经验总结
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)