当前位置:网站首页>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;
}
边栏推荐
- Single instance mode of encapsulating PDO with PHP in spare time
- Modeling specifications: naming conventions
- Who is the winner of PTA
- What is the investment value of iFLYTEK, which does not make money?
- 4. File modification
- My C language learning record (blue bridge) -- on the pointer
- Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
- Some problem records of AGP gradle
- Codeforces 5 questions par jour (1700 chacune) - jour 6
- MPLS experiment
猜你喜欢
随机推荐
[pointer training - eight questions]
Technology sharing | what if Undo is too big
指针笔试题~走近大厂
多态day02
Rust language -- iterators and closures
适合程序员学习的国外网站推荐
Audio audiorecord binder communication mechanism
resulttype和resultmap的区别和应用场景
Data and Introspection__ dict__ Attributes and__ slots__ attribute
Shell 传递参数
canvas切积木小游戏代码
#PAT#day10
Four logs of MySQL server layer
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
[ruoyi] set theme style
Overview of OCR character recognition methods
Tomb. Weekly update of Finance (February 7 - February 13)
Problems encountered in 2022 work IV
Linear programming matlab
MySQL advanced notes








