当前位置:网站首页>008 C语言基础:C判断
008 C语言基础:C判断
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。
C 语言把任何非零和非空的值假定为 true,把零或 null 假定为 false。
二:if语句
一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。
if(boolean_expression){
/*如果布尔表达式为真将执行的语句*/
}
if…else语句
一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。
if(){
}else{
}
注意区分,和Python语法有区别:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
二:switch语句
允许测试一个变量等于多个值时的情况。每个值称为一个case,且被测试的变量会对每个switch case进行检查。
语法:
switch(expression){
case constant-expression :
statement(s);
break; /* 可选的 */
case constant-expression :
statement(s);
break; /* 可选的 */
/* 您可以有任意数量的 case 语句 */
default : /* 可选的 */
statement(s);
}
switch 语句必须遵循下面的规则:
- switch 语句中的 expression 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
- 在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
- case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。
- 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
- 当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
- 不是每一个 case 都需要包含 break。如果 case 语句不包含 break,控制流将会 继续 后续的 case,直到遇到 break 为止。
- 一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。
实例:
#include <stdio.h>
int main(){
char grade = 'B';
switch(grade){
case 'A':
printf("很棒! \n");
case 'B':
case 'C':
printf("做得好! \n");
case 'D':
printf("通过了 \n");
case 'E':
printf("未通过 \n");
default:
printf("请重试 \n");
}
printf("您的成绩是:%c \n", grade); // 这里如果是%d,则会输出66,对应acsii表的B
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim switch.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc switch.c -o switch
┌──(rootkali)-[~/Desktop/c_test]
└─# ./switch
做得好!
您的成绩是:B
三:?:运算符
Exp1 ? Exp2 : Exp3;
其中,Exp1、Exp2 和 Exp3 是表达式。请注意,冒号的使用和位置。
? 表达式的值是由 Exp1 决定的。如果 Exp1 为真,则计算 Exp2 的值,结果即为整个 ? 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,结果即为整个 ? 表达式的值。
边栏推荐
- Matlab | visualization of mathematical properties related to three interesting circles
- Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
- Agile development - self use
- 021 C语言基础:递归,可变参数
- WPF open source control library extended WPF toolkit Introduction (Classic)
- 苹果唯一图谱架构常识
- PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
- 电商产品如何在知乎上进行推广和打广告?
- NestJS环境变量配置,解决如何在拦截器(interceptor)注入服务(service)的问题
- Penetration test - directory traversal vulnerability
猜你喜欢
Kotlin compose implicitly passes the parameter compositionlocalprovider
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
Promise source code class version [III. promise source code] [detailed code comments / complete test cases]
跟着BUU学习Crypto(周更)
Agile development - self use
Matlab | visualization of mathematical properties related to three interesting circles
There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused
如何系统学习LabVIEW?
【Unity】UI交互组件之按钮Button&可选基类总结
2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
随机推荐
手撸promise【二、Promise源码】【代码详细注释/测试案例完整】
Knowledge of iPhone certificate structure
windows上安装MySQL
Static timing analysis OCV and time derive
WPF open source control library extended WPF toolkit Introduction (Classic)
Kotlin compose implicitly passes the parameter compositionlocalprovider
Matlab | visualization of mathematical properties related to three interesting circles
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
Fplan powerplan instance
Fplan power planning
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
Installation of low code development platform nocobase
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
敏捷开发篇--Agile Development-自用
List of best reading materials for machine learning in communication
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
Method of decoding iPhone certificate file
苹果手机证书构体知识
Promise [II. Promise source code] [detailed code comments / complete test cases]