当前位置:网站首页>C -- switch case statement
C -- switch case statement
2022-07-28 03:19:00 【Dusk and starry sky】
c#——switch case sentence
c# Medium switch case There are three structures in a sentence , The specific form is shown in the figure below :
(1)Switch The first structure of :( For example 1)
switch(i)
case 1:
//
break;
case2:
//
break;
example 1
[csharp] view plaincopyprint?
namespace Switch
{
class Program
{
static void Main(string[] args)
{
int i = 2;
switch(i)
{
case 2:
Console.WriteLine(“ You really are 2!”);
Console.WriteLine(“ You are so talented !”);
break;
case 4:
Console.WriteLine(“ Go to hell. !”);
break;
case 8:
Console.WriteLine(“ Hair. Hair !”);
break;
}
Console.ReadKey();
}
}
}
(2)Switch The second structure of :
switch(i)
case 1:
//
break;
case2:
//
break;
default:
//
break;
example 2
[csharp] view plaincopyprint?
namespace Switch
{
class Program
{
static void Main(string[] args)
{
int i = 9;
switch(i)
{
case 2: // amount to if(if==2)
Console.WriteLine(“ You really are 2!”);
Console.WriteLine(“ You are so talented !”);
break; //C# You have to write break
case 4:
Console.WriteLine(“ Go to hell. !”);
break;
case 8:
Console.WriteLine(“ Hair. Hair !”);
break;
default: // amount to if Of the statement else
Console.WriteLine(“ What you entered {0} It makes no sense ”,i);
break;
}
Console.ReadKey();
}
}
}
Be careful :C# Medium switch Statement must be written break, No writing , But there is one case except , Amalgamated case situation , Don't write break.( For example 3):
Switch The third structure of : Amalgamated case situation , To omit break.
switch(i)
case 1:
case2:
//
break;
example 3:
[csharp] view plaincopyprint?
namespace Switch
{
class Program
{
static void Main(string[] args)
{
int i = 200;
switch(i)
{
case 2: // amount to if(if==2)
Console.WriteLine(“ You really are 2!”);
Console.WriteLine(“ You are so talented !”);
break; //C# You have to write break
case 4:
Console.WriteLine(“ Go to hell. !”);
break;
case 8:
Console.WriteLine(“ Hair. Hair !”);
break;
/*
case 100:
Console.WriteLine(“ What you input is the whole money !”);
Console.WriteLine(“ You are rich ”);
break;
case 200:
Console.WriteLine(“ What you input is the whole money !”);
Console.WriteLine(“ You are rich ”);
break;
*/
// The code above is equivalent to the code below
case 100:
case 200: // amount to if(i=100||i=200), The only one case No need to break The situation of
Console.WriteLine(" What you input is the whole money !");
Console.WriteLine(" You are rich ");
break;
default: // amount to if Of the statement else
Console.WriteLine(" What you entered {0} It makes no sense ",i);
break;
}
Console.ReadKey();
}
}
}
Be careful :switch In the sentence case The value of can only be 2,4,“aaa” Constant , It can't be a variable 、 expression . ( For example 4)
example 4
[csharp] view plaincopyprint?
namespace Switch
{
class Program
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
int i = Convert.ToInt32(s1);
int k = 10;
switch(k)
{
case i: // error :case The median can only be used 2,4,“aaa” Constant , You can't write variables
Console.WriteLine(“ What you input is the same as what the program assumes !”);
break; //C# You have to write break
}
Console.ReadKey();
}
}
}
summary :switch Statement and if The difference between sentences :
● Greater than or equal to (>=)、 Less than or equal to (<=) For judgment if sentence , And equal to (=) For judgment switch sentence .
● switch Statement case Be similar to if…else…else if…else, But the judgment of discrete value .
( The judgment of discrete value is considered to be equal to the judgment of the situation ).
● switch Generally, it can be used if rewrite , however if Not necessarily switch rewrite ( For example 2).
● Don't forget to break.C# in break It's impossible not to write , Except for the merger case The situation of ( For example 3).
● case The value in must be a constant , It can't be a variable 、 expression ( For example 4).
边栏推荐
- Tungsten Fabric SDN — BGP as a Service
- Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment
- 综合 案例
- WEB安全基础 - - -命令执行漏洞
- 基于OpenCV的轮廓检测(3)
- [2022 Niuke multi school 2 K link with bracket sequence I] bracket linear DP
- Talk about the speech synthesis function of Baidu University of science and technology news Feiyun Zhisheng
- bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
- Games101 review: ray tracing
- 力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
猜你喜欢

QT official example: Fridge Magnets example

RBD块存储设备的扩容以及缩容操作(六)

静态博客搭建工具汇总

并发编程面试题总结

Uniapp - make phone calls and send text messages

在线问题反馈模块实战(十六):实现查详情功能

意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!

OA项目之我的审批(会议查询&会议签字)

【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程

max_pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
随机推荐
在线问题反馈模块实战(十六):实现查详情功能
els 定时器
Summary of static blog building tools
Comprehensive case
stm32F407-------DSP学习
Random forest and integration method learning notes
工程地质实习-工程地质 题集
工程电磁场复习基本知识点
Digital twin technology drives smart factory to reduce burden and enhance operation and maintenance benefits
[nature of class (in Objective-C language)]
[stream] basic knowledge of stream
C#中关闭窗体的四种方法
嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践
STM32之IO模拟串口篇
VI command details
酒店vr全景展示拍摄提供更多合作和洽谈的机会
“讳疾忌医”的开源走不远
Design and practice of unified security authentication for microservice architecture
Redis群集
The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,