当前位置:网站首页>C language learning
C language learning
2022-07-01 12:53:00 【51CTO】
Branches and loops (1)
Text
One . Branch statements and loop statements
Branch statement
if switch
Loop statement
while for do while go to sentence
What is a sentence ?
c There is a semicolon in language (;) Separated by a statement .
1. Branch statement ( Selection structure )
1).if sentence
if The grammatical structure of a sentence :
if( expression ) or if( expression )
sentence ; person sentence 1;
else
sentence 2;
Multiple branches
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
else
sentence 3;
If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )
int
main()
{
int
age
=
111;
if (
age
<
18)
printf(
" A minor \n");
else
if (
age
>=
18
&&
age
<
20)
//&& Representation logic and , If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )
printf(
" teenagers \n");
else
if (
age
>=
20
&&
age
<
50)
printf(
" Prime of life \n");
else
if (
age
>=
50
&&
age
<
90)
printf(
" aged \n");
else
printf(
" Old man does not die \n");
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
If the condition holds , To execute multiple statements , Code blocks should be used
#include <stdio.h>
int main()
{
if( expression )
{
Statement list 1;
}
else
{
Statement list 2
}
return 0;
}
int
main()
{
int
age
=
111;
if (
age
<
18)
{
printf(
" A minor \n");
// Statement list 1
printf(
" Can't fall in love \n");
// You can print multiple printf
}
else
{
if (
age
>=
18
&&
age
<
20)
printf(
" teenagers \n");
else
if (
age
>=
20
&&
age
<
50)
// Statement list 2
printf(
" Prime of life \n");
else
if (
age
>=
50
&&
age
<
90)
printf(
" aged \n");
else
printf(
" Old man does not die \n");
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
there {} It's just a code block
In the air else
// Use properly {} It can make the logic of the code clearer
// Code style is important
practice : Output 1~100 Between the odd numbers
2).switch sentence
switch The grammatical structure of a sentence :
switch( Integer expression )
{
Statement item ;
}
What are statement items ?
// It's some case sentence
// as follows :
case Integral constant expression :
sentence ;
//switch There can be if sentence
int
main()
{
int
day
=
0;
scanf(
"%d",
&
day);
//& Fetch address , Take out variables day
if (
1
==
day)
// Judgment variable day Is it equal to 1, If yes, output on Monday
printf(
" Monday \n");
else
if(
2
==
day)
printf(
" Tuesday \n");
else
if (
3
==
day)
printf(
" Wednesday \n");
else
if (
4
==
day)
printf(
" Thursday \n");
else
if (
5
==
day)
printf(
" Friday \n");
else
if (
6
==
day)
printf(
" Saturday \n");
else
if (
7
==
day)
printf(
" Sunday \n");
return
0;
}
// use else if...else if More complicated , use switch Multi branch statements are relatively simple
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
After the change
int
main()
{
int
day
=
0;
//day For integer variables
scanf(
"%d",
&
day);
switch (
day)
//day Is an integer expression
{
case
1:
//case 1(1 Is a constant variable expression ):
printf(
" Monday \n");
break;
//break It means to end the cycle
case
2:
printf(
" Tuesday \n");
break;
case
3:
printf(
" Wednesday \n");
break;
case
4:
printf(
" Thursday \n");
break;
case
5:
printf(
" Friday \n");
break;
case
6:
printf(
" Saturday \n");
break;
case
7:
printf(
" Sunday \n");
break;
}
return
0;
}
// summary : Easy to understand
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
stay switch Statement break:
stay switch In the sentence , We can't Branch , With break You can realize the real Branch .
practice : Input 1~5, Output “weekday”; Output 6~7, Input “weekend”
int
main()
{
int
day
=
0;
scanf(
"%d",
&
day);
switch(
day)
{
case
1:
case
2:
case
3:
case
4:
case
5:
printf(
"weekday\n");
break;
case
6:
case
7:
printf(
"weekend\n");
break;
default:
//default Can be placed in switch Anywhere , When switch The value of the expression is not equal to case The value of the tag , This is the time default The following statement will execute .
printf(
" Input error \n");
break;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
break The actual effect of the statement is to divide the statement list into different parts .
default Clause (default and case There is no order , It can be used anywhere )
If the expressed value is the same as all case What if the value of the tag is different ?
It's nothing , The result is that all statements are skipped ; The program will not terminate , No errors reported , Because this situation is c It is not suitable to report errors .
What if you don't want to ignore the values of expressions that don't match all tags ?
You can add an item to the statement list default Clause , Put the label below default: Write in any one case Where labels can appear ( When switch The value of the expression does not match all case The value of the tag , This default The statement after the clause will be executed . all , Every switch Only one... Can appear in the statement default Clause , But it can appear anywhere in the statement list , And the statement flow will run through case The label runs through default Clause .)
practice :
int
main()
{
int
n
=
1;
int
m
=
2;
switch (
n)
{
case
1:
m
++;
case
2:
n
++;
case
3:
switch (
n)
{
//switch Allow nesting
case
1:
n
++;
case
2:
m
++;
n
++;
break;
}
case
4:
m
++;
break;
default:
break;
}
printf(
"m=%d,n=%d\n",
m,
n);
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
1. Loop statement
while for do while
1).while sentence
while Grammatical structure :(while sentence , Loop can be realized )
while( expression )
Loop statement ;
practice : Print... On the screen 1~10
break stop it
continue skip , Make the entry judgment of the next cycle
Conclusion :continue It means a dead cycle ( The cycle is not over )
边栏推荐
- oracle cdc 数据传输时,clob类型字段,在update时值会丢失,update前有值,但
- 运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
- 科学创业三问:关于时机、痛点与重要决策
- 题目 1004: 母牛的故事(递推)
- R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
- Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
- Zabbix 6.0 源码安装以及 HA 配置
- codeforces -- 4B. Before an Exam
- 【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
- 我选的热门专业,四年后成了“天坑”
猜你喜欢

mysql统计账单信息(下):数据导入及查询

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现

工具箱之 IKVM.NET 项目新进展

项目部署,一点也不难!

VM虚拟机配置动态ip和静态ip访问

【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)

Different test techniques

redis探索之缓存击穿、缓存雪崩、缓存穿透

硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

Feign & Eureka & Zuul & Hystrix 流程
随机推荐
What are the solutions for session sharing of highly paid programmers & interview questions series 118?
shell脚本导入存储过程到数据库
How to play with the reading and writing operations of blocking sockets?
Simple Fibonacci (recursive)
买卖其实也有风险
How can genetic testing help patients fight disease?
Three stages of aho
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
c语言学习
【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
图灵奖得主Judea Pearl:最近值得一读的19篇因果推断论文
Zero copy technology of MySQL
Redis explores cache consistency
腾讯总考epoll, 很烦
華為面試題: 招聘
Use Net core access wechat official account development
使用nvm管理nodejs(把高版本降级为低版本)
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
Shell script imports stored procedures into the database
6.30 simulation summary