当前位置:网站首页>008 C language foundation: C judgment
008 C language foundation: C judgment
2022-06-27 04:24:00 【Prison plan progress 50%】
List of articles
One : summary
Judgment structures require programmers to specify one or more conditions to evaluate or test , And the statement to execute when the condition is true ( Essential ) And the statement to execute when the condition is false ( Optional ).
C Language assumes that any non-zero and non null value is true, Put zero or null Is assumed to be false.
Two :if sentence
One if sentence Consists of a Boolean expression followed by one or more statements .
if(boolean_expression){
/* The statement that will be executed if the Boolean expression is true */
}
if…else sentence
One if sentence You can follow with an optional else sentence ,else Statement is executed when the Boolean expression is false .
if(){
}else{
}
Pay attention to distinguish between , and Python There is a difference in grammar :
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
Two :switch sentence
Allows testing when a variable is equal to multiple values . Each value is called a case, And the variables to be tested will be applied to each switch case Inspection .
grammar :
switch(expression){
case constant-expression :
statement(s);
break; /* Optional */
case constant-expression :
statement(s);
break; /* Optional */
/* You can have any number of case sentence */
default : /* Optional */
statement(s);
}
switch Statements must follow the following rules :
- switch Statement expression Must be an integer or enumeration type , Or a class type , among class There is a single conversion function that converts it to an integer or enumeration type .
- In a switch There can be any number of case sentence . Every case Followed by a value to compare and a colon .
- case Of constant-expression Must be with switch Variables in have the same data type , And it must be a constant or literal quantity .
- When the variable being tested equals case When the constant in ,case The following statement will be executed , Until I met break The statement so far .
- When you meet break When the sentence is ,switch End , The control flow will jump to switch The next line after the statement .
- Not every one case All need to include break. If case The statement does not contain break, The control flow will continue Follow up case, Until I met break until .
- One switch Statements can have an optional default case, Appear in the switch Ending .default case Can be used on all of the above case To perform a task when it's not true .default case Medium break Statements are not required .
example :
#include <stdio.h>
int main(){
char grade = 'B';
switch(grade){
case 'A':
printf(" great ! \n");
case 'B':
case 'C':
printf(" well done ! \n");
case 'D':
printf(" Passed \n");
case 'E':
printf(" Not through \n");
default:
printf(" Please try again \n");
}
printf(" Your score is :%c \n", grade); // If this is %d, Will output 66, Corresponding acsii Tabular B
return 0;
}
result :
┌──(rootkali)-[~/Desktop/c_test]
└─# vim switch.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc switch.c -o switch
┌──(rootkali)-[~/Desktop/c_test]
└─# ./switch
well done !
Your score is :B
3、 ... and :?: Operator
Exp1 ? Exp2 : Exp3;
among ,Exp1、Exp2 and Exp3 Is an expression . Please note that , Use and location of colons .
? The value of the expression is determined by Exp1 Decisive . If Exp1 It's true , The calculation Exp2 Value , The result is the whole ? Value of expression . If Exp1 For false , The calculation Exp3 Value , The result is the whole ? Value of expression .
边栏推荐
- 016 C语言基础:C语言枚举类型
- Semantic version 2.0.0
- 通信中的机器学习最佳阅读资料列表
- math_ Number set (number set symbol) and set theory
- LDR6028 手机设备一边充电一边OTG传输数据方案
- 015 C语言基础:C结构体、共用体
- Ledrui ldr6035 usb-c interface device supports rechargeable OTG data transmission scheme.
- 为什么 C# 访问 null 字段会抛异常?
- fplan-Powerplan实例
- 【B站UP DR_CAN学习笔记】Kalman滤波1
猜你喜欢

math_ Number set (number set symbol) and set theory
![[BJDCTF2020]The mystery of ip](/img/f8/c3a7334252724635d42c8db3d1bbb0.png)
[BJDCTF2020]The mystery of ip

Microservice system design -- microservice monitoring and system resource monitoring design

快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码

跟着BUU学习Crypto(周更)

LDR6028 手机设备一边充电一边OTG传输数据方案

Learn crypto from Buu (Zhou Geng)

Microservice system design -- microservice invocation design

渗透测试-目录遍历漏洞

真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......
随机推荐
微服务系统设计——服务熔断和降级设计
[BJDCTF2020]The mystery of ip
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
021 C语言基础:递归,可变参数
PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
Fplan layout
Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码
【B站UP DR_CAN学习笔记】Kalman滤波1
Microservice system design -- distributed cache service design
733. image rendering
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
Learn crypto from Buu (Zhou Geng)
文旅夜游|以沉浸式视觉体验激发游客的热情
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
007 C语言基础:C运算符
IOS development: understanding of dynamic library shared cache (dyld)
LDR6028 手机设备一边充电一边OTG传输数据方案
卷积神经网络(CNN)网络结构及模型原理介绍
渗透测试-文件上传/下载/包含