当前位置:网站首页>[C language - zero foundation lesson 7] sequential structure and selection structure
[C language - zero foundation lesson 7] sequential structure and selection structure
2022-07-27 09:15:00 【Super Daxiong】
Preface
Blogger :Super Daxiong ( A cute new blogger )
C Language column :https://blog.csdn.net/m0_64857213/category_11678637.html
LeetCode special column :https://blog.csdn.net/m0_64857213/category_11691019.html
This issue is about C Language order structure and choice structure , If there is any mistake, you are welcome to put forward your views .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !Click on the I Start registration Study 、 Brush problem
Catalog
Import
Life comes in order , No dress rehearsal . From our birth to our school 、 Then we started working 、 marry 、 Have children and move towards middle age 、 Finally, we bid farewell to the world in old age. We come step by step in order. Generally, there will be no newly born old people ,C The same is true of sequential structures in languages .
Life is made up of many choices , Some choices are our active choices, of course, some choices are also choices we are forced to make . Whether you want it or not, you have to make a choice . for example : You buy a house in a big city in order to stand out , You can choose to do
Do business to make money 、 You can choose to be a programmer to make money 、 You can also choose to study hard and make money with your knowledge 、 You can also run a taxi 、 delivery 、 As long as delivery is not illegal . This is the selection structure in the sequential structure of our chapter . I want to ask you ? You learn C Language is also a choice you make on your own initiative or forced to make .
Just like life ,C There are also choices in the program ,C Language provides two kinds of choice statements .
If and switch
Sequential structure
The following is a code with sequential structure , From top to bottom .
int a,b,c;
a=1;
b=2;
c=3;Selection structure
Before learning the choice structure, we need to understand the following C Logical values in language
stay C Language in Africa 0 Treat as positive ,0 Be false
Relational operator
>、<、== be equal to 、!= It's not equal to 、>=、<=
If it is true , If not, it is false
printf("%d",3 > 5);result 0
printf("%d",3 <5);result 1
Logical operators
! Not && And || or
priority !>&&>||
associativity :! From left to right &&|| From right to left
Logical expression
&&
0&&1=0 1&&0=0 0&&0=0 1&&1=0
||
0||1=1 1||0=1 0||0=0 1||1=0
!
!0=1 !1=0
printf("%d\n",!3);
printf("%d\n", !0);
printf("%d\n", -1&&0);
printf("%d\n", -1 && 1);
printf("%d\n", 5 && 0);
printf("%d\n", -1 || 0);
printf("%d\n", -1 || 1);
printf("%d\n", 5 || 0);
printf("%d\n", 0 || 0);result
0
1
0
1
0
1
1
1
0
If Conditional statements
Single branch
Format :if( Conditions ) {
sentence ;
}
if (5>2) {
printf(" having dinner ");
}result : having dinner
if (0>2) {
printf(" having dinner ");
}result :
Conditions not established
If the condition is true, execute the statement in the statement block , If it is not established, it will not be implemented
Double branch
Format :if( Conditions ) {
sentence 1;
}else{
sentence 2;
}
if (0>2) {
printf(" having dinner ");
}
else {
printf(" sleep ");
}result : sleep
If the condition is true, execute the statement in the statement block , If the condition does not hold, execute else Statement block in
Multiple branches
Format :if( Conditions ) {
sentence 1;
}else if(){
sentence 2;
} else if(){
sentence 2;
} else if(){
sentence n;
}
else{
}
int a = 2;
if (a==0) {
printf(" having dinner ");
}
else if(a == 1) {
printf(" sleep ");
}
else if (a == 2) {
printf(" Blogging ");
}else{
printf(" Don't blog ");
}result : Blogging
Execute the statement according to the judgment condition
If Sentences can be omitted else
Switch Conditional statements
Format :
switch( expression ){
case Constant 1: sentence ; (break;)
case Constant 2: sentence ;(break;)
case Constant n: sentence ;(break;)
default: Constant n+1 sentence ;
}
switch( expression ) The inside of the expression must be character and integer expressions .
case Constant 1、2、3 Equal sum default They all serve as labels .
break; Jump out of switch Code block .
default: If the expression judges case If all constants are not satisfied, execute default The statement in . There can be no default, If case Constant If there is no match with the condition form, it will jump out switch Code block .
First judge the value in the expression , Is it equal to case Constant in . If equal to, execute case The statement in ; After execution, check whether the right break The statement ends with , No, break Then proceed until you encounter break Statement or execution completed switch Everything in the code block .
int a = 0;
switch (a) {
case 0: printf(" zero \n");
case 1: printf(" One \n");
case 2: printf(" Two \n");
case 3: printf(" 3、 ... and \n");
default: printf(" other \n");
}result :
zero
One
Two
3、 ... and
other
int a = 0;
switch (a) {
case 0: printf(" zero \n");break;
case 1: printf(" One \n");
case 2: printf(" Two \n");
case 3: printf(" 3、 ... and \n");
default: printf(" other \n");
}result :
zero
int a = 0;
switch (a) {
case 0: printf(" zero \n");
case 1: printf(" One \n");
case 2: printf(" Two \n");
case 3: printf(" 3、 ... and \n");break;
default: printf(" other \n");result :
zero
One
Two
3、 ... and
int a = 5;
switch (a) {
case 0: printf(" zero \n");
case 1: printf(" One \n");
case 2: printf(" Two \n");
case 3: printf(" 3、 ... and \n");
default: printf(" other \n");result :
other
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
边栏推荐
- CUDA programming-02: first knowledge of CUDA Programming
- Nutrecipes developed based on arkui ETS
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
- The lifecycle of arkui development framework components
- Summary of traversal methods
- ArkUI中的显式动画
- As a VC, the auction house invested Web3 for the first time
- ES6 new - string part
- Sharing of four open source face recognition projects
- QT uses SQLite to open multiple database files at the same time
猜你喜欢

Aruba learning notes 10 security authentication portal authentication (web page configuration)

ES6 new - deconstruction assignment of array / object

C# 窗体应用常用基础控件讲解(适合萌新)

【进程间通信IPC】- 信号量的学习

CUDA programming-05: flows and events

"Weilai Cup" 2022 Niuke summer multi school training camp 1

Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry

对 int 变量赋值的操作是原子的吗?

The execution sequence of async/await, macro tasks and micro tasks

ES6 new - array part
随机推荐
Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
What if the parameters in QT are structs or custom classes when sending signals?
DNS域名空间
Nut weather
Is the operation of assigning values to int variables atomic?
npm和yarn 更新依赖包
Restful
[cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
基于restful页面数据交互
Matlab uses m file to produce fuzzy controller
Function anti chattering throttling
PyTorch自定义CUDA算子教程与运行时间分析
MySQL basic knowledge learning (I)
SQL exercise set
B tree
Tensorflow loss function
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
Antdesign a-modal自定义指令实现拖拽放大缩小
Some practical, commonly used and increasingly efficient kubernetes aliases
Linux Installation and remote connection MySQL records