当前位置:网站首页>分支语句和循环语句
分支语句和循环语句
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;
}
边栏推荐
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- VLSM variable length subnet mask partition tips
- KF UD decomposition pseudo code implementation advanced [2]
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- 服务未正常关闭导致端口被占用
- Fgui project packaging and Publishing & importing unity & the way to display the UI
- 【干货】提升RTK模糊度固定率的建议之周跳探测
- Exception: ioexception:stream closed
- Several high-frequency JVM interview questions
猜你喜欢

10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache

Role movement in the first person perspective

FairyGUI增益BUFF數值改變的顯示
![[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal](/img/11/ee0628a68542236fc641966579a31a.png)
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal

Wechat applet development experience

阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
![[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
![[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k

3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法

Mixed use of fairygui button dynamics
随机推荐
【干货】提升RTK模糊度固定率的建议之周跳探测
国企秋招经验总结
Novatel board oem617d configuration step record
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
Answer to "software testing" exercise: Chapter 1
KF UD分解之伪代码实现进阶篇【2】
Fairygui loop list
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
染色法判定二分图
阿里云一面:并发场景下的底层细节 - 伪共享问题
Agile development helps me
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
wsl常用命令
Combination of fairygui check box and progress bar
Knowledge system of digital IT practitioners | software development methods -- agile
Halcon knowledge: gray_ Tophat transform and bottom cap transform