当前位置:网站首页>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 Bnesting 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;
}
边栏推荐
- Era5 reanalysis data download strategy
- Leetcode problem solving -- 98 Validate binary search tree
- Mysql database operation
- 1. Dynamic parameters of function: *args, **kwargs
- Performance analysis of user login TPS low and CPU full
- Modeling specifications: naming conventions
- How to choose PLC and MCU?
- 深入探究指针及指针类型
- Safety science to | travel, you must read a guide
- Mysqldump data backup
猜你喜欢

Era5 reanalysis data download strategy

SAP ALV单元格级别设置颜色

11. Container with the most water

js凡客banner轮播图js特效

Mysqldump data backup

Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration

Problems encountered in 2022 work IV

What is the investment value of iFLYTEK, which does not make money?

Game theory matlab

My C language learning record (blue bridge) -- on the pointer
随机推荐
jsscript
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
Polymorphic day02
1003 emergency (25 points), "DIJ deformation"
My C language learning record (blue bridge) -- on the pointer
Modeling specifications: naming conventions
SD卡報錯“error -110 whilst initialising SD card
Safety science to | travel, you must read a guide
Precautions for single chip microcomputer anti reverse connection circuit
[kubernetes series] learn the exposed application of kubernetes service security
Arabellacpc 2019 (supplementary question)
JS音乐在线播放插件vsPlayAudio.js
11. Container with the most water
Derivation of anti Park transform and anti Clarke transform formulas for motor control
js 正则过滤和增加富文本中图片前缀
Audio-AudioRecord Binder通信机制
StrError & PERROR use yyds dry inventory
Analyze 菜单分析
[ruoyi] set theme style