当前位置:网站首页>Branch and loop statements in C language learning
Branch and loop statements in C language learning
2022-07-24 23:45:00 【Xiao Tao who likes programming】
Branch and loop statements
Branch statement
Branch statements are also called conditional judgment statements
- if
- switch
if The grammatical structure of a sentence
- if ( expression )
sentence ; - if( expression )
sentence 1;
else
sentence 2; - if ( expression 1)
sentence 1;
if else ( expression 2)
sentence 2;
else
sentence 3;
If the expression is true, the statement executes
C In language 0 Said the false , Not 0 Said really
And that is else With the nearest one if Matchswitch The grammatical structure of a sentence
switch( Integer expression )switch It is often used to deal with multi branch situations
{
case Integral constant expression :
sentence ;
}
- stay switch Statement break
Such as :
#include <stdio.h>
int main()
{
int day = 6;
switch (day)
{
case 1:
printf(" Monday \n");
break;
case 2:
printf(" Tuesday \n");
break;
case 3:
printf(" Wednesday \n");
break;
case 4:
printf(" Thursday \n");
break;
case 5:
printf(" Friday \n");
break;
case 6:
printf(" Saturday \n");
break;
case 7:
printf(" Sunday \n");
break;
}
return 0;
}
stay switch Collocation in sentences break Implement branch
break sentence The practical effect is to divide the statement list into different branches .
default Clause
default:
It can be written in any case The front and back of the car .
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 .
Every switch Only one... Can appear in the statement default Clause .
Loop statement
- while
- for
- do while
while loop
while( expression )
Loop statement ;
for example :
Print... On the screen 1~10 The number of .
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0;
}
It's basically like this .
for loop
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 .
for example :
Use for loop Print... On the screen 1-10 The number of .
#include <stdio.h>
int main()
{
int i = 0;
//for(i=1/* initialization */; i<=10/* Judgment part */; i++/* Adjustment part */)
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0;
}
do while loop
do Sentence syntax :
do
Loop statement ;
while( expression );
for example :
Use do while Loop printing 1~10 The number of .
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d\n", i);
}while(i<10);
return 0;
}
do while The characteristic of the statement is that the loop executes at least once , First execute the statement once, and then judge whether the expression is true
In a loop statement break and continue
In the loop, whenever you encounter break, Terminate the cycle directly , The cycle is over , No more cycles .
continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed , Instead, jump directly to the judgment part of the loop statement , Make the entry judgment of the next cycle .
for example :
Use continue;
#include <stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
i = i + 1;
if (i == 5)
continue;
printf("%d ", i);
}
return 0;
Its output is 
Use break;
#include <stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
i = i + 1;
if (i == 5)
break;
printf("%d ", i);
}
return 0;


come on. !!!
边栏推荐
- How painful is it to write unit tests? Can you do it
- 常用在线测试工具集合
- Install Kaspersky 2018 under win server 2012 R2
- QT | event system qevent
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 5
- [zero basis] SQL injection for PHP code audit
- BGP related knowledge points
- Live broadcast preview | online seminar on open source security governance models and tools
- VGA display based on FPGA
猜你喜欢

VGA display based on FPGA

谢振东:公共交通行业数字化转型升级的探索与实践

Notes of Teacher Li Hongyi's 2020 in-depth learning series lecture 1

Network Security Learning (V) DHCP

From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7

Be an artistic test / development programmer and slowly change yourself

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code

Network Security Learning (II) IP address

Paper notes: accurate causal influence on discrete data
随机推荐
How to put long links into Excel
Effect evaluation of generative countermeasure network
Use SQLite provided by the system
Be an artistic test / development programmer and slowly change yourself
云图
Financial products can reach 6%. I want to open an account to buy financial products
Implementation of cat and dog data set classification experiment based on tensorflow and keras convolutional neural network
Routing policy in republishing
Use of serial queues
How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
Upgrade the jdbc driver to version 8.0.28 and connect to the pit record of MySQL
Qt | 事件系统 QEvent
SQL rewriting Series 6: predicate derivation
采坑记录:TypeError: 'module' object is not callable
常用在线测试工具集合
LP liquidity pledge mining system development detailed procedure
Code coverage
Network Security Learning (II) IP address
SQLite database operation
See project code Note 1