当前位置:网站首页>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 的值,结果即为整个 ? 表达式的值。
边栏推荐
- [BJDCTF2020]The mystery of ip
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-10 personal solutions
- 2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答
- MySQL development environment
- 1.5 conda的使用
- 2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers
- 高等数学(第七版)同济大学 习题1-10 个人解答
- Installation of low code development platform nocobase
- 语义化版本 2.0.0
- Kotlin compose implicitly passes the parameter compositionlocalprovider
猜你喜欢

低代码开发平台NocoBase的安装

办公室VR黄片,骚操作!微软HoloLens之父辞职!

Building lightweight target detection based on mobilenet-yolov4

JMeter distributed pressure measurement

MobileNet系列(4):MobileNetv3网络详解

Static timing analysis OCV and time derive

Système de collecte des journaux
![Basic functions of promise [IV. promise source code]](/img/a0/8f28ec8951088b8e66e7079432f326.png)
Basic functions of promise [IV. promise source code]

手撸promise【二、Promise源码】【代码详细注释/测试案例完整】

How to make ef core 6 support dateonly type
随机推荐
Argo workflows - getting started with kubernetes' workflow engine
USB DRIVER
jmeter分布式压测
2021:adavqa: overlapping language priors with adapted margin cosine loss *
Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
Installation of low code development platform nocobase
2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
Building lightweight target detection based on mobilenet-yolov4
Common sense of Apple's unique map architecture
【promise一】promise的介绍与手撸的关键问题
文旅夜游|以沉浸式视觉体验激发游客的热情
021 C语言基础:递归,可变参数
静态时序分析-OCV和time derate
Log collection system
Penetration test - directory traversal vulnerability
WPF open source control library extended WPF toolkit Introduction (Classic)
016 C语言基础:C语言枚举类型
卷积神经网络(CNN)网络结构及模型原理介绍
Système de collecte des journaux
2016Analyzing the Behavior of Visual Question Answering Models