📄 chapter 2.mht
字号:
style=3D"mso-tab-count: =
2"> &nbs=
p; =20
</SPAN>F();<o:p></o:p></P>
<P class=3DCprg><SPAN=20
style=3D"mso-tab-count: =
1"> =20
</SPAN>}<o:p></o:p></P>
<P class=3DCprg><SPAN=20
style=3D"mso-tab-count: =
1"> =20
</SPAN>else {<o:p></o:p></P>
<P class=3DCprg><SPAN=20
style=3D"mso-tab-count: =
2"> &nbs=
p; =20
</SPAN>G();<o:p></o:p></P>
<P class=3DCprg><SPAN=20
style=3D"mso-tab-count: =
1"> =20
</SPAN>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">This block of if =
statements is=20
copied straight from the documentation to denote an idea. The above two =
ifs are=20
the same. What we want to highlight here is that the brackets are =
optional; the=20
visual look and feel can get misleading at times.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Switch =
Statement</SPAN></B><SPAN style=3D"COLOR: =
windowtext"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>zzz a =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>a.abc(1);<o:p></o:p></P>
<P class=3DCprg>a.abc(10);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("zero");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("one");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>default:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("end");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>one<o:p></o:p></P>
<P class=3DCoutput>end<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A switch statement is =
a=20
substitute to multiple ifs as both work in the same fashion. It is a =
matter of=20
style in deciding on the use of them. We use the time of day to decide =
which=20
statement to use when and where. In other words, alike if, the switch =
also=20
brings intelligence to our programs. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">The switch statement =
checks the=20
value of the variable i against the values given with the case =
statement. If any=20
one value matches, all the statements within the case upto the break =
statement=20
are executed. If none of the case statements match, the default =
statement is=20
executed. Remember it is not mandatory to have a default statement with =
a break.=20
The value of the variable in question decides on the code to be=20
executed.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U> <o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("zero");<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("one");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(8,1): error CS0163: Control cannot fall through =
from one=20
case label ('case 0:') to another<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">The case statement =
has its own=20
set of complex rules. The first one says that every case must end with a =
break=20
statement; thereupon preventing it from encroaching into the next case. =
Don't we=20
have railing to prevent us from falling over our balcony? Thus, there is =
no=20
natural way invented to execute code within two case statements at the =
same=20
time. Most people consider it to be an unnecessary restriction C# has =
placed on=20
the usage of the case statement. Like everything in life, there is a =
workaround.=20
But we will elaborate on that later.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>zzz z =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>z.abc(0);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("second");<o:p></o:p></P>
<P class=3DCprg>goto case 1;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("first");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>second<o:p></o:p></P>
<P class=3DCoutput>first<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">We explained that one =
case could=20
not fall through another case but if it is imperative then you can use =
the goto=20
statement. This statement accepts a case statement as a parameter and =
then=20
simply jumps to it. Thus, after executing the code of one case, we can =
jump to=20
another case and execute its code.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>zzz z =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>z.abc(0);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("second");<o:p></o:p></P>
<P class=3DCprg>goto case 1;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("hi");<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("first");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compiler Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(15,1): warning CS0162: Unreachable code=20
detected<o:p></o:p></P>
<P class=3DCoutput><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>second<o:p></o:p></P>
<P class=3DCoutput>first<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Here we have confused =
the=20
compiler to no end and we advise you not to write such code. At first, =
the=20
compiler informs you that no lines of code after a goto will ever be =
called and=20
thus generates an unreachable code warning. </SPAN><SPAN=20
style=3D"COLOR: red"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">But then the control =
seeps to the=20
case statement directed by goto and forgets that it was falling through =
a case=20
statement from another. We are not trying to belittle the compiler but =
can it=20
not please remember goto statements that we write and stop looking =
stupid in=20
front of our eyes.</SPAN><SPAN style=3D"COLOR: =
red"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>zzz z =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>z.abc(0);<o:p></o:p></P>
<P class=3DCprg>z.abc(3);<o:p></o:p></P>
<P class=3DCprg>z.abc(1);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("zero");<o:p></o:p></P>
<P class=3DCprg>goto case 3;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("one");<o:p></o:p></P>
<P class=3DCprg>goto default;<o:p></o:p></P>
<P class=3DCprg>case 3:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("two");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>default:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("last");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>zero<o:p></o:p></P>
<P class=3DCoutput>two<o:p></o:p></P>
<P class=3DCoutput>two<o:p></o:p></P>
<P class=3DCoutput>one<o:p></o:p></P>
<P class=3DCoutput>last<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">To prevent a fall =
through, every=20
case has to end either with a goto or a break. With a<SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>break statement, no other code =
gets=20
called but with a goto, the code to be executed depends entirely on the =
case it=20
jumps to. The above code is the ideal way to write code.=20
However=85<o:p></o:p></SPAN></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -