当前位置:网站首页>[C language switch branch statement and loop statement]
[C language switch branch statement and loop statement]
2022-07-27 05:37:00 【I'm really not a second dimension】
C Language switch Branch statements and loop statements
Type conversion
Only data of the same type can be operated , If the data of different types needs to be converted to the same type before calculation .
1. Automatic type conversion :
Conversion rules :(1,2,3 For priority )
On the basis of not losing data , Some space can be sacrificed appropriately
1、 Less bytes to more bytes
char int
2、 Signed to unsigned
3、 Integer to floating point
Be careful :char short If you operate with different types of data , Will be promoted to int Participate in the operation after type
2. Cast :
( New type name ) data ;
Data may be lost in this way , Careful use
switch Switch branch statement
switch(n) //n It can be a number 、 expression , The result of the operation must be an integer
{
case val: // If val==n, Then turn on the execution switch
... //val Must be constant
break; // Turn off the actuator switch . end swicth
// If each case After all add break, Formed a branch structure
//switch Cannot be used in continue
//case a ... b: Can represent [a,b], But only GNU The compiler supports this syntax , Not recommended
case val1:
...
default:
// If all case It didn't open , Then finally open default, And it can be finally opened in any position
}
for Loop statement
Loop is a way to make code execute repeatedly , Achieve the effect you want .
for Circulation is a very flexible 、 Varied and dangerous ( Compared with other cycles ) The cycle of
for A loop usually uses a variable to guide the execution of the loop , This variable is called a loop variable i
for([1];[2];[3])
{
[4]
}
[1]、 Assign initial values to cyclic variables ,C99 The above criteria can define the cyclic variable here -std=gnu99
[2]、 Determine whether the loop variable reaches the boundary , If it doesn't arrive , Into the loop
[4]、 Code that is executed repeatedly , Become a circular body .
[3]、 Changing the value of a loop variable , Prevent dead cycle , Generally, cyclic variables are self added 、 Self reduction
for Cycle through various expressions :
for(;;) // Dead cycle
{
}
int i=0;
for(; i<10;i++)
{
}
for(int i=0; ;i++)
{
if(i>=10)
{
break;
}
}
for(int i=0; i<10;)
{
...
i++;
}
while Loop statement
while( expression )
{
// The loop body
}
Execute loop body when expression is true , Judge the expression again after execution , And so on , Until the expression is false , End of cycle .
while The cycle is equivalent to for Reduced version of for(; expression ;)
for Loop is specially responsible for solving the problem of clearly knowing the number of cycles
while Cycle is specially responsible for solving the problem of uncertain cycle times
do-while Loop statement
do{
// The loop body
}while( expression );// Expression is true cyclic continuation , False to end the loop
Execute the loop first , And then judge the cyclic condition , The loop body is executed at least once
Suitable for working first 、 Special circumstances of post inspection , For example, enter a password 、 Judging the content of the document
A nested loop
Loop statements contain loop statements
The outer loop is executed once , Memory loop execution n Time
边栏推荐
猜你喜欢

Hi3516DV300环境搭建

背景图片相关应用-铺满,自适应

分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)

JVM part I: memory and garbage collection part II -- class loading subsystem

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制

初识C语言——常量、变量

Li Hongyi machine learning team learning punch in activity day04 - Introduction to deep learning and back propagation mechanism

Rolling Division

强制登录,七牛云上传图片

创建项目 实现登录注册,生成jwt,发送验证码
随机推荐
redis发布订阅模式
下载url-loader,用limit指定图片大小后,显示不出图片
Three waiting methods of selenium and three processing methods of alert pop-up
【codeforces round#800 B. Paranoid String】DP
Li Hongyi machine learning team learning punch in activity day05 --- skills of network design
秒杀系统设计
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
md5 密码加密
后台实现spu管理
Collation of several difficult methods in pytorch --gather & squeeze & unsqueeze
接收方设置并发量和限流
C语言做一个小迷宫
The concept of cloud native application and 15 characteristics of cloud native application
How to get started quickly and strengthen learning?
Trying to evolve_ My first CSDN blog
蓝图-类视图方法
后台频道组管理功能实现
cookie增删改查和异常
Li Hongyi machine learning team learning punch in activity day02 --- return
GCC 编译选项