当前位置:网站首页>C language judgment, ternary operation and switch statement usage
C language judgment, ternary operation and switch statement usage
2022-07-06 03:17:00 【Don't be confused afterwards】
Catalog
? : Operator ( Ternary operator )
Judgment statement
C Language provides the following types of judgment statements .
sentence | describe |
---|---|
if sentence | One if sentence Consists of a Boolean expression followed by one or more statements . |
if...else sentence | One if sentence You can follow with an optional else sentence ,else Statement is executed when the Boolean expression is false . |
nesting if sentence | You can be in a if or else if Use another if or else if sentence . |
switch sentence | One switch Statement allows testing when a variable is equal to more than one value . |
nesting switch sentence | You can be in a switch Use another switch sentence . |
? : Operator ( Ternary operator )
The general form is as follows :
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 value of the entire expression . If Exp1 For false , The calculation Exp3 Value , The result is the value of the entire expression .
switch sentence
One switch Statement allows testing when a variable is equal to more than one value . Each value is called a case, And the variables to be tested will be applied to each switch case Inspection .
grammar
C In language switch Sentence syntax :
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 Is a constant expression , Must be 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 ()
{
/* Definition of local variables */
char grade = 'B';
switch(grade)
{
case 'A' :
printf(" great !\n" );
break;
case 'B' :
case 'C' :
printf(" well done \n" );
break;
case 'D' :
printf(" You passed \n" );
break;
case 'F' :
printf(" Better try again \n" );
break;
default :
printf(" Invalid grades \n" );
}
printf(" Your score is %c\n", grade );
return 0;
}
When the above code is compiled and executed , It will produce the following results :
well done
Your score is B
nesting switch sentence
You can put a switch As an external switch Part of a sequence of statements , That is, it can be in a switch Use another switch sentence . Even inside and outside switch Of case Constants contain common values , There's no contradiction .
grammar
C In language nesting switch Sentence syntax :
switch(ch1) {
case 'A':
printf(" This A It's the outside switch Part of " );
switch(ch2) {
case 'A':
printf(" This A It's internal switch Part of " );
break;
case 'B': /* Inside B case Code */
}
break;
case 'B': /* external B case Code */
}
// example
//switch Statement nesting , Test length of service reward applet !
#include<stdio.h>
int main(void)
{
char sex;
int age ;
printf(" Please enter your gender abbreviation ! male (M), Woman (F)\n");
scanf_s("%c", &sex);
switch (sex)
{
case 'M':
case 'm':
printf(" Your gender is “ male ” Please enter the test !\n");
printf(" Please enter your length of service !\n");
scanf_s("%2d",&age);
switch (age)
{
case 5:
printf(" Reward iphone a !!\n");
break;
case 10:
printf(" Reward a car !!\n");
break;
case 15:
printf(" Reward a small building !!\n");
break;
default:
printf(" I'm sorry , Fail to meet the reward conditions or exceed the length of service !!\n");
break;
}
break;
case 'F':
case 'f':
printf(" Your gender is “ Woman ” Please enter the test !\n");
printf(" Please enter and exit your seniority !\n");
scanf_s("%2d",&age);
switch (age)
{
case 5:
printf(" Reward iphone a !\n");
break;
case 10:
printf(" Reward a set of famous brand cosmetics !\n");
break;
case 15:
printf(" Reward Hermes with a bag !\n");
break;
default:
printf(" I'm sorry , Fail to meet the reward conditions or exceed the length of service !!\n");
break;
}
break;
}
return 0;
}
边栏推荐
- Detailed use of dbutils # yyds dry goods inventory #
- Single instance mode of encapsulating PDO with PHP in spare time
- Reverse repackaging of wechat applet
- Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
- 这些不太会
- 教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
- [ruoyi] ztree custom icon (iconskin attribute)
- Selenium share
- SD卡报错“error -110 whilst initialising SD card
- Redis cache breakdown, cache penetration, cache avalanche
猜你喜欢
随机推荐
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
NR modulation 1
My C language learning records (blue bridge) -- files and file input and output
Résumé des méthodes de reconnaissance des caractères ocr
Tomb. Weekly update of Finance (February 7 - February 13)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
tcpdump: no suitable device found
[kubernetes series] learn the exposed application of kubernetes service security
Function knowledge points
js凡客banner轮播图js特效
1003 emergency (25 points), "DIJ deformation"
深入探究指针及指针类型
指针笔试题~走近大厂
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
ArabellaCPC 2019(补题)
Is there a completely independent localization database technology
暑期刷题-Day3
Technology sharing | what if Undo is too big
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
继承day01