当前位置:网站首页>[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 !
边栏推荐
- [interprocess communication IPC] - semaphore learning
- 易语言编程: 让读屏软件可获取标签控件的文本
- CUDA programming-01: build CUDA Programming Environment
- Primary function t1744963 character writing
- How to study happily on station B?
- NPM and yarn update dependent packages
- 存储和计算引擎
- NPM install error forced installation
- Explanation of binary tree
- Data interaction based on restful pages
猜你喜欢

巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...

软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1

Pytorch custom CUDA operator tutorial and runtime analysis

Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)

【微服务~Sentinel】Sentinel之dashboard控制面板

一些实用、常用、效率越来越高的 Kubernetes 别名

5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises

ES6 new - array part

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

音乐体验天花板!14个网易云音乐的情感化设计细节
随机推荐
ES6 new - string part
[cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
[flutter -- geTx] preparation
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
PVT's spatial reduction attention (SRA)
SQL exercise set
[interprocess communication IPC] - semaphore learning
QDoubleValidator不生效问题解决办法
DNS域名空间
Summary of traversal methods
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
How to upload dynamic GIF map in blog
Qdoublevalidator does not take effect solution
Interface test tool - JMeter pressure test use
Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry
Explanation of common basic controls for C # form application (suitable for Mengxin)
Music experience ceiling! Emotional design details of 14 Netease cloud music
ES6 new - function part
Replace restricts the text box to regular expressions of numbers, numbers, letters, etc
Interface test tool -postman usage details