当前位置:网站首页>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).
边栏推荐
- C#WinForm开发:如何将图片添加到项目资源文件(Resources)中
- [uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 2/100
- Scheme sharing | experts gather to jointly explore accent AI speech recognition
- [stream] parallel stream and sequential stream
- 什么是虚函数?
- ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation
- ssm整合(整合配置)
- Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
- [2022 Niuke multi school 2 K link with bracket sequence I] bracket linear DP
- “讳疾忌医”的开源走不远
猜你喜欢

53. Maximum Subarray最大子数组和

JVM memory layout detailed, illustrated, well written!

stm32F407-------DSP学习

傅里叶级数

The test post changes jobs repeatedly, jumping and jumping, and then it disappears

CAD创建组却没有组合在一起?

Tungsten Fabric SDN — BGP as a Service

关于权重衰退和丢弃法

Uniapp——拨打电话、发送短信

Original title of Blue Bridge Cup
随机推荐
[2022 Niuke Game 2 J question link with arithmetic progress] three part set three part / three part extreme value / linear equation fitting least square method
上位机与MES对接的几种方式
汇总了50多场面试,4-6月面经笔记和详解(含核心考点及6家大厂)
【ACwing 1064 小国王】状压dp
C#设置Textbox控件不可编辑
会议OA项目之我的审批&&签字功能
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发2/100
Redis持久化机制
[email protected] Annotation usage
Redis经典面试题总结
stm32F407-------FPU学习
Review basic knowledge points of engineering electromagnetic field
Contour detection based on OpenCV (3)
RBD块存储设备的扩容以及缩容操作(六)
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,
On weight decay and discarding method
R 笔记 MICE
The test post changes jobs repeatedly, jumping and jumping, and then it disappears
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
并发编程面试题总结