当前位置:网站首页>Branch and loop statements
Branch and loop statements
2022-07-28 07:13:00 【So and so Xiao Lu】
- There are nine kinds of control statements .
It can be divided into three categories :
- Conditional judgment statements are also called branch statements :if sentence 、switch sentence ;
- Loop statement :do while sentence 、while sentence 、for sentence ;
- Turn to statement :break sentence 、goto sentence 、continue sentence 、return sentence .
- Select statement ( Judgment statement )
if sentence
- Format :if( expression )
sentence 1;
else
sentence 2;
When the value of the expression is non 0 When ( It's true ), Execute statement 1, Otherwise, the result is 0( For false ), perform else The sentence of 2.
- Under normal circumstances, the statement 1 And statement 2 There is not only one statement , Instead, there are multiple statements .
Multiple use curly brackets ({}) Cover up , As a statement 1 Or words 2;
- Be careful ;else Always with the nearest one and not with others else Paired if sentence
for example :

The following is the correct matching method

- Don't confuse == and =
==( Double equal sign ) It's about judging whether it's equal ,= It's assignment
- Conditional expression
Format : expression 1? expression 2: expression 3
It represents if expression 1 It's true , Execute expression 2, For false , Execute expression 3.
switch sentence
- Format :
switch( Integer expression )
{
case Integer constant expression :
sentence ;
}
- Under normal circumstances , We can't use to complete the real Branch , So we use break load case Statement to implement the real Branch ;

We can also put a few case Close , Share one statement , To save code

- If the expressed value is the same as all case What if the values of the tags don't match ?
When switch The value of the expression does not match all case The value of the tag , This default The statement following the clause will execute .
therefore , 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 execute a case Execute as tag default Clause .
- Loop statement
while sentence
- Format :
while( expression )
Loop statement ;
- continue stay while The role of the loop is :
continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed ,
It's a jump to while The judgment part of a sentence . Make the entry judgment of the next cycle .
- example : Dichotomy search

for sentence
- for( expression 1; expression 2; expression 3)
Loop statement ;
expression 1
expression 1 For the initialization part , Used to initialize loop variables .
expression 2
expression 2 For the conditional judgment part , Used to determine when a loop ends .
expression 3
expression 3 For the adjustment part , For adjustment of cyclic conditions
- example :
#include
int main()
{
char arr1[] = "welcome to bit...";
char arr2[] = "#################";
int left = 0;
int right = strlen(arr1)-1;
printf("%s\n", arr2);
for (left=0, right=strlen(src)-1;
left
left++, right--)
{
Sleep(1000); This is window Function parameters under , In milliseconds .
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf( "%s\n", target);
}
retutn 0;
}
do while sentence
- do
Loop statement ;
while( expression );
Circular statements , If there is not only one , It needs to be enclosed in curly brackets
- i++ stay continue Behind , Lead to an endless cycle .


break and continue sentence
- break The statement is to jump out of this cycle .
- continue The statement is to skip this loop , The next few times continue , But it is prone to dead circulation , There will be different results , If you pay attention to continue The positional relationship between statements and loop statements .
Guess the number game to achieve ( Half search algorithm )
example :


rand Is a function that generates random values , The header file is #include .time(null) It's a function of time , The header file is time.h,time You need to put a pointer in the parentheses of the function , utilize null Null pointer , Come on
Form a constantly changing timestamp , To keep changing the value of random values .rand You also need to quote a srand function .
边栏推荐
- Blueberry pie Bluetooth debugging process
- Open virtual machine kali2022.2 and install GVM
- easypoi一对多,合并单元格,并且根据内容自适应行高
- Redis哨兵模式及集群
- PXE无人值守安装管理
- 起点中文网 字体反爬技术 网页可以显示数字字母 网页代码是乱码或空格
- Standard C language learning summary 9
- Easypoi export interlaced style settings
- Implementation method of converting ast into word vector before converting word vector
- Construction of Yum warehouse
猜你喜欢

三层交换和VRRP

NAT-网络地址转换

easypoi导出表格带echars图表

MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays

Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank

Static and floating routes

Easypoi export interlaced style settings

YUM仓库的搭建

Clock tree analysis example

Canvas drawing 1
随机推荐
Standard C language learning summary 3
232(母)转422(公)
Es6--- > arrow function, class, modularization
小甲鱼C(第六章数组1、2)
Result fill in the blank (dfs*c language)
MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation
shell---sed语句练习
Wangeditor (@4.7.15) - lightweight rich text editor
Standard C language learning summary 6
Results fill in the blank. How many days of national day are Sundays (solved by pure Excel)
PXE unattended installation management
VSphere esxi 7.0 update 3 release notes
Canvas drawing 1
Method of decomposing path into directory name and file name
MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
VNC Timed out waiting for a response from the computer
Anaconda3 cannot open navigator solution
Blueberry pie Bluetooth debugging process
Shell--- function
[learning records of erudite Valley] Super summary, attentive sharing | collection