当前位置:网站首页>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. !!!
边栏推荐
- Go基础笔记_4_map
- Burp's thinking from tracing to counteracting
- Entity service is an anti pattern
- 凸优化基础知识
- [nuxt 3] (x) runtime configuration
- Paper time review MB2: build a behavior model for autonomous databases
- See project code Note 1
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- 技术操作
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 5
猜你喜欢

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

基于FPGA的VGA显示

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge

Convert a string to an integer and don't double it

给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享

Are you still using system. Currenttimemillis()? Take a look at stopwatch

Understanding complexity and simple sorting operation
Browser cache

Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points

50 places are limited to open | with the news of oceanbase's annual press conference coming!
随机推荐
Are you still using system. Currenttimemillis()? Take a look at stopwatch
Opengauss kernel analysis: query rewriting
Digital stopwatch based on Verilog HDL
Lidar obstacle detection and tracking: CUDA European clustering
Development direction and problems of optaplanner
Xiezhendong: Exploration and practice of digital transformation and upgrading of public transport industry
郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究
See project code Note 1
salesforce零基础学习(一百一十六)workflow -&gt; flow浅谈
Shell echo command
Notes of Teacher Li Hongyi's 2020 in-depth learning series 9
Qt | 事件系统 QEvent
网上怎么炒股手机上炒股安全吗
中金证券新课理财产品的收益有百分之六吗?我想要开户理财
Value driven provides the core driving force for the transformation of commercial BP - BP realization under business scenarios - Commercial BP sharing
常用在线测试工具集合
How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
SQL result export function. If you click the work order but don't enter it, the interface is always blank and there is no response. What should you do?
谢振东:公共交通行业数字化转型升级的探索与实践
Network Security Learning (I) virtual machine