当前位置:网站首页>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 .
边栏推荐
- Construction of Yum warehouse
- Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec
- Generate create table creation SQL statement according to excel
- Implementation method of Bert
- freemarker合并单元格,if、else标签的使用,null、空字符串处理
- VNC Timed out waiting for a response from the computer
- Esxi community network card driver updated again
- MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类
- DOM -- event chain, event bubble and capture, event proxy
- MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
猜你喜欢

爬虫学习总结

PXE unattended installation management

Understanding of maximum likelihood estimation, gradient descent, linear regression and logistic regression

Static and floating routes

Canvas drawing 2

Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec

Icc2 analysis timing artifact analyze_ design_ violations

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

DOM operation cases

DHCP service
随机推荐
freemarker导出word,带表格和多张图片,解决图片重复和变形
DHCP服务
三层交换和VRRP
DNS域名解析
Small turtle C (Chapter 6 arrays 1 and 2)
Nrf51822 review summary
Shell --- conditional statement practice
232 (female) to 422 (male)
MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation
DOM - Events
MySQL build database Series (I) -- download MySQL
SySeVR环境配置:joern-0.3.1、Neo4j-2.1.5、py2neo2.0
Serial port configuration of raspberry pie
Understanding of C language EOF
Shell--- function
Clock tree analysis example
ES6 add -- > object
shell---条件语句练习
Easypoi one to many, merge cells, and adapt the row height according to the content
Canvas drawing 2