当前位置:网站首页>C language - control statement
C language - control statement
2022-07-28 20:11:00 【Boring ~】
C Language is a structured language , also C Language is to solve things in life .
Control statement is the execution flow of control program , Implement various structural ways of the program .
Programs are used to solve problems in life , A program is made up of various statements , Control statements are used to control the execution flow of a program , Implement various structural ways of the program . Control statements are auxiliary statements , The program needs to realize the problems in life , So the program needs to realize these structural ways in life , So the control statement helps the program Realization These structural modes , So it's called Control statement .
Everything in our life can be abstracted into one of three structures , Or some combination of these .
With these three structures, we can describe things in our life .
C Language is for writing programs , The program is written to solve things in our life , Let things in life be solved by computers , So at this point C Language must be able to describe these structures , To provide our program , therefore C Language produces control statements , This is it. C The power of language , Be able to describe these structures .
C Language can describe these structures , So say ,C Language is a structured programming language .
Program - solve , Realize things in life
Things in life can be abstract , Split 1 In three structural ways ( The order , choice , loop )
The procedure is made by C Write in language .
therefore C Language should provide these abilities , Be able to describe these structures in life .
Use this way C The program written in language can describe all kinds of things in life .
Program is to describe things in life , When something in life needs to be done repeatedly , But when doing it repeatedly , I find that I don't need to do it repeatedly , The result has been obtained , So in order for the program to realize such functions ,C Language provides break sentence ,break Statement can make the program end the loop , When will the cycle end , You need control statements to control break sentence , Choose here if -else sentence . When something in life is being done repeatedly , At a certain repetition , Don't want to do the next thing , Want to skip the next thing , Doing it again , For example, a person has to eat every day , rest , This is a repetitive thing , But every Friday , Don't want to rest , So skip the rest , Wait for Friday , Just keep repeating it .
getchar
That is, you are constantly learning in life , Keep learning this thing over and over , But in the process of learning , Met a rich woman , Then the repetition of learning will be stopped , I won't study anymore , That is to stop the cycle .
That's because the program is to realize things in life , So the program must also be able to implement this way , therefore C Language provides break sentence , When in a loop , If a certain condition is reached , Then use break sentence , It will end the cycle , Like in life , If there is a rich woman , Will stop learning , Reached a certain condition .
break Application scenarios of , If a certain condition is reached, the cycle will be terminated
If a rich woman appears in life , Will stop learning , Rich woman is that condition .
continue Application scenarios of , If a certain condition is reached , Just skip the statement after this loop , Go straight to the next cycle .
For example, you are playing the game of clearance , If you are in the second level , dead , Are you unable to perform the remaining tasks in the second level , Return to the second level , Come back .
Death is the condition , When a certain condition is reached , Just skip the loop .
Because the program is to realize things in life , So the program should be able to implement this way , therefore C Language provides programs with continue sentence , When executed continue sentence , Will skip this cycle , To the next cycle .
and break and continue sentence , It can be executed , It can also be done without , To control execution , You need to use control statements
And when you meet a rich woman in life, you will stop learning , If you don't meet a rich woman, you will continue to study
That program in order to achieve such a way , How does it control to stop learning and continue learning ?
Use control statements to control execution , Then stop learning , How should this be achieved ?
C Language developed break sentence , You can stop , therefore C Language uses control statements to control break Statements, etc .
getchar Functions and scanf Function reads data in the input buffer , When there is no data in the input buffer , Will wait for input , After typing data on the keyboard , Press enter and the data will be compared with \n, Put in the input buffer ,getchar Function can only read one character at a time ,scanf Function can read multiple characters , When a space is encountered , Will stop , Or no space , Read \0 Previous data .
getchar() The function reads a character into the input buffer , Characters to be read , Return character's ascii value ,ascii Value is number , So the number returned , Put it in int Type variables , Instead of char Variable of type , because char The space of a variable of type is only one byte , And the type of number is int Type , It takes four bytes , So you have to use int Type of variable to receive .
And after input error or reading , Will return a EOF, and EOF yes -1, It's also numbers , So we must use int Type of variable to receive .
So how to make getchar Stop ?
Press on the keyboard ctrl+z,getchar After reading the characters we entered , It will return EOF
After pressing other data on the keyboard ,getchar After reading our characters , Characters will be returned ascii value .
When you return , because ascii Values and EOF It's all integers , So use integer variables to receive , Instead of using character variables to receive , Because you want to receive integers instead of characters .
putchar Is the output function
The principle of keyboard input :
When we press the key on the keyboard , Press enter after , It will put the corresponding letter of the key we pressed into the input buffer , If you don't press enter , It will not be put into the input buffer , The character corresponding to the Enter key is \n, So after input , The input buffer will have a string of characters + The Enter key corresponds to \n.
scanf The function is where the data is read , Not read directly from the keyboard , Instead, read in the input buffer , And can't read \n, But when encountering a space character, it will stop , Only read before or \n Previous characters , Picky eaters .
getchar Where the function reads data , Also read in the input buffer , It can only read one character at a time , And not picky about food , encounter \n, It can also read .
So when we use scanf After the function reads the data , The input buffer will leave a \n, If there's something in the back getchar function , Will read directly \n.
But if we don't want getchar read \n. Want him to read other values , So use an extra getchar function , Let him read it first \n, At this point, the input buffer is clean , nothing in the world ,getchar Waiting for input , We type on the keyboard , Push the input character into the input buffer ,getchar In the reading .
As long as there are characters in the input buffer , No matter what character it is ,getchar Metropolis Read To , The characters that will be entered into the buffer take come out , then getchar Function after reading , It will return characters ascii value .
If there are other characters in the input buffer , But we don't want this getchar Read , So let these characters be used by the rest getchar Read , After reading , It's our turn getchar. Then the rest getchar When will you finish reading ? If getchar After reading , The value returned is EOF, If it is EOF Will stop reading , Will jump out of the reading cycle ,
C How does a language stop a loop , Use break. So how to control the stop cycle ? Use control statements to control , Then how to stop the cycle ? Use break sentence , So use control statements to control break sentence .
You can cycle or not cycle , that C How language controls circulation and non circulation ? Use control statements , If you meet the conditions, cycle , Stop the cycle if the conditions are not met , So you can use ifelse sentence .
that C How does language achieve non cyclic ? Use break sentence , You can stop the cycle .
Everything in life can be abstracted into three structural ways ( The order , choice , loop )
C Program written in language , To realize the problems in life , Solve problems in life .
That program in order to achieve this structure , So there are control statements , It is an auxiliary statement , Help the program realize these structural ways .
There are many choices in life , Add bits , You can mess it up , You can study hard , You can also exit .
therefore C Language provides choice statements , Realize these options , This is one of the control statements , By controlling other statements to achieve several structural ways .
When implementing the selection structure , Use ifelse Statement to control other statements to implement the selection structure , When conditions are met , Just execute some statements , When the conditions are not met , Do not execute certain statements , The selection structure is realized .
You need to keep learning in life , This is a repeated process . It's a circular structure
The program needs to implement this structure , A circular statement appears , If the conditional expression is satisfied , It's going to keep cycling , Until not satisfied , Stop the cycle , Go through the loop statement control Other statements implement loop structures .
How do loop statements, as control statements, control other statements ? If the conditional expression is satisfied 1 Will cycle , If you are not satisfied, you will not cycle .
When learning repeatedly , Met a rich woman , Here comes the choice , Program usage if-else The control statement implements this selection structure , When conditions are met , Just execute some statements , If you don't meet the conditions, you won't execute .
After meeting a rich woman , There's no need to learn , Just jump out of the repetitive process of learning , There is no need to repeat , End this cycle .
In order to realize the way that the program can jump out of the loop , Use break Statement can realize this way .
When playing games in customs clearance , Reached the second level , You were killed , We need to play the second level again , Skip the level after the second level , Restart the second level .
In order to realize this way , Use continue This can be achieved .
When entering data on the keyboard , Finally, press enter , This data will be sent to the input buffer , The character corresponding to the Enter key is also sent to the buffer .
scanf The function reads data in the input buffer , It is picky about food , Space encountered , perhaps \n, Will not read data
getchar The function also reads data in the input buffer , It is not picky about food , Any character , He can read , But call once , Only one character can be read .
Read means to take away the characters in the input buffer , It's not a copy .
gets Functions are also read functions , It makes up for scanf The disadvantages of functions : Picky food , It can read spaces , But I can't read \n.
They are all functions , Just call them , Will execute ,getchar Only one data can be read at a time ,scanf Function can read single data , You can also read a string of characters .getchar Poor ability , Read only one ,scanf You can choose to read one , You can also read several .
getchar Function after reading characters , What is returned is the character of ascii value , And reading errors , Or after reading , The return is EOF,EOF Namely -1, So all the returned values are integers , To be stored in an integer variable , If the returned value is character , Then put it into a character variable .
Everything in life can be abstracted as a choice structure , Sequential structure , Loop structure
So everything in life can be abstracted as one of them , Or a combination of several .
The program chooses the structure in order to realize ,C Language provides ifelse sentence , adopt if and else Conditional expression in , Satisfy expression , execute , Not satisfied, not executed , Constitute a selection structure , So as to realize the selection structure .
In order to realize the loop structure ,C Language provides for,while,do-while sentence , So as to realize the circular structure
Some things in life need to be done repeatedly , Form a circular structure , Use loop statements to achieve , Use circular statements to control Repeat other statements , Implement loop structure , Satisfy the cyclic condition , Just cycle , It doesn't satisfy the cyclic condition , Just exit the loop
When you choose not to do it again , It forms the selection structure , Need to use if-else Statement to implement the selection structure .
Don't repeat , To exit the loop , In order to exit the loop ,C Language provides break sentence , By using ifelse Statement to control break sentence .
边栏推荐
- Common modules of saltstack
- [experience] some suggestions and experience on repairing electronic equipment
- 5. Difference between break and continue (easy to understand version)
- zfoo增加类似于mydog的路由
- [C language] scanf format input and modifier summary
- Deploy LNMP automatically with saltstack
- Source insight project import and use tutorial
- Data system of saltstack
- Design of air combat game based on qtgui image interface
- C language operators and input and output
猜你喜欢

English translation Italian - batch English translation Italian tools free of charge

3、 Are formal and actual parameters in a programming language variables?

河北:稳粮扩豆助力粮油生产提质增效
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

A chip company fell in round B

adb remount of the / superblock failed: Permission denied

What is the process of swing event processing?

Const pointer of C language and parameter passing of main function

The privatized instant messaging platform protects the security of enterprise mobile business

Communication learning static routing across regional networks
随机推荐
Edge detection and connection of image segmentation realized by MATLAB
flask_ Mail source code error
KPMG China: insights into information technology audit projects of securities fund management institutions
Crawl IP
CDGA|工业互联网行业怎么做好数据治理?
Advanced notes (Part 2)
Labelme(一)
Tencent cloud deployment lamp_ Experience of building a station
Saltstack advanced
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
Can China make a breakthrough in the future development of the meta universe and occupy the highland?
There are five certificates in the soft examination advanced examination, which is more worth taking?
【实验分享】CCIE—BGP反射器实验
C language array and bubble sort
Redis notes
2、 Relationship between software operation and memory
[C language] print pattern summary
Deploy LNMP automatically with saltstack
Rand function generates pseudo-random numbers