当前位置:网站首页>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;
}
边栏推荐
- 电机控制反Park变换和反Clarke变换公式推导
- Redis cache breakdown, cache penetration, cache avalanche
- Inherit day01
- Linear programming matlab
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- Web security SQL injection vulnerability (1)
- Overview of OCR character recognition methods
- 张丽俊:穿透不确定性要靠四个“不变”
- February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
- IPv6 comprehensive experiment
猜你喜欢
NR modulation 1
Linear programming matlab
Explore pointers and pointer types in depth
11. Container with the most water
Derivation of anti Park transform and anti Clarke transform formulas for motor control
蓝色样式商城网站页脚代码
Reverse repackaging of wechat applet
MySQL advanced notes
IPv6 jobs
JS音乐在线播放插件vsPlayAudio.js
随机推荐
银行核心业务系统性能测试方法
如何做好功能测试
Tidb ecological tools (backup, migration, import / export) collation
Eight super classic pointer interview questions (3000 words in detail)
Redis cache breakdown, cache penetration, cache avalanche
Data and Introspection__ dict__ Attributes and__ slots__ attribute
How to choose PLC and MCU?
Leetcode problem solving -- 99 Restore binary search tree
Codeforces 5 questions par jour (1700 chacune) - jour 6
2022工作中遇到的问题四
[unity3d] GUI control
蓝色样式商城网站页脚代码
11. Container with the most water
OCR文字識別方法綜述
Selenium share
three.js网页背景动画液态js特效
An article about liquid template engine
Technology sharing | what if Undo is too big
XSS challenges绕过防护策略进行 XSS 注入
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project