当前位置:网站首页>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;
}
边栏推荐
- 【概念】Web 基础概念认知
- XSS challenges绕过防护策略进行 XSS 注入
- Redis cache breakdown, cache penetration, cache avalanche
- Single instance mode of encapsulating PDO with PHP in spare time
- Tomb. Weekly update of Finance (February 7 - February 13)
- My C language learning record (blue bridge) -- under the pointer
- 2022工作中遇到的问题四
- Getting started with applet cloud development - getting user search content
- Mysql database operation
- Installation and use tutorial of cobaltstrike-4.4-k8 modified version
猜你喜欢
JS regular filtering and adding image prefixes in rich text
【RISC-V】外部中断
MySQL Server层四个日志
Era5 reanalysis data download strategy
Overview of OCR character recognition methods
How to do function test well
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
SAP ALV颜色代码对应颜色(整理)
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
随机推荐
How to do function test well
[ruoyi] set theme style
适合程序员学习的国外网站推荐
Overview of OCR character recognition methods
Performance analysis of user login TPS low and CPU full
暑期刷题-Day3
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
SAP ALV颜色代码对应颜色(整理)
八道超经典指针面试题(三千字详解)
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
Distributed service framework dobbo
My C language learning record (blue bridge) -- on the pointer
jsscript
记录一下逆向任务管理器的过程
Prototype design
Item 10: Prefer scoped enums to unscoped enums.
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
Function knowledge points
Crazy, thousands of netizens are exploding the company's salary
What are the principles of software design (OCP)