📄 chapter 2.mht
字号:
<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>goto aa;<o:p></o:p></P>
<P class=3DCprg>aa: ;<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=3DCbase><SPAN style=3D"COLOR: windowtext">Just by placing a =
semicolon, we=20
bring a big smile on the C# virtual face. ;)<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">Loops=20
<o:p></o:p></SPAN></B></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">In all, we have four =
iterative=20
statements, the for, foreach, while and the last kid on the block, the =
do=20
while.<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>public static void Main() {<o:p></o:p></P>
<P class=3DCprg>int i =3D 1;<o:p></o:p></P>
<P class=3DCprg>do<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + " ");<o:p></o:p></P>
<P class=3DCprg>i++;<o:p></o:p></P>
<P class=3DCprg>} while ( i <=3D 3);<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>1 2 3<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">Earlier we learnt =
about the=20
differences between the for and the while and the scientific reasons =
behind=20
choosing one of them. Now we add a twist and introduce the do while. =
This is=20
identical to the while. There is just one and only one difference =
between them.=20
Minor but could be life-threatening at times. In a while, the condition =
is=20
evaluated at the beginning of the loop whereas in the case of the do =
while it is=20
at the end.<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>public static void Main() {<o:p></o:p></P>
<P class=3DCprg>int i =3D 1;<o:p></o:p></P>
<P class=3DCprg>do<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + " ");<o:p></o:p></P>
<P class=3DCprg>i++;<o:p></o:p></P>
<P class=3DCprg>} while ( i <=3D 0);<o:p></o:p></P>
<P class=3DCprg>i =3D 1;<o:p></o:p></P>
<P class=3DCprg>while ( i <=3D 0 ) {<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + "...");<o:p></o:p></P>
<P class=3DCprg>i++;<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>1<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">In the do statement, =
the loop=20
condition is checked at the end of the first iteration. Thus, the loop =
is=20
executed at least once. In the above case, the condition i <=3D 0 is =
false at=20
the outset as the variable i is given a value of 1. In spite of this, =
the do=20
executes once, the condition is patently false and we quit out.=20
<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">For the while, the =
condition is=20
checked at the very beginning and thus as the condition is false, the =
code in=20
the while is never called.<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>for ( int i =3D 1; i <=3D 10 ; i++)<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + "...");<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i);<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 Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(7,22): error CS0103: The name 'i' does not exist =
in the=20
class or namespace 'zzz'<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">Any variable created =
in the for=20
has a lifetime or scope within the embedded statements only. The =
variable i does=20
not exist beyond the for statements.<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>for ( int i =3D 1; ; i++)<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + "...");<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=3DCbase><SPAN style=3D"COLOR: windowtext">Please, please do not =
run the=20
above program as it will never ever stop unless you Ctrl-C it. The for =
condition=20
if present must result in a boolean. Having a numeric data type in =
between the=20
two semi colons would raise the red flag for the compiler and result in =
an=20
error. If the boolean expression is omitted, then C# assumes the value =
to be=20
true by default. Hence the above code never ever =
stops.<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>int [] a =3D new int[3]{1,2,3};<o:p></o:p></P>
<P class=3DCprg>foreach ( int i in a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(i + " ");<o:p></o:p></P>
<P class=3DCprg>i =3D 10;<o:p></o:p></P>
<P class=3DCprg>zzz.abc(ref i);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static void abc(ref int j)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.Write("abc " + j + " ");<o:p></o:p></P>
<P class=3DCprg>j =3D 100;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<SPAN style=3D"COLOR: red"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN=20
style=3D"COLOR: red"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U><SPAN style=3D"COLOR: black">Compiler=20
Error</SPAN></U><o:p></o:p></P>
<P class=3DCoutput>a.cs(9,1): error CS1604: Cannot assign to 'i' because =
it is=20
read-only<o:p></o:p></P>
<P class=3DCoutput>a.cs(10,13): error CS1605: Cannot pass 'i' as a ref =
or out=20
argument because it is read-only<o:p></o:p></P>
<P class=3DCbase><SPAN=20
style=3D"COLOR: red"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase>We are not allowed to change the iteration variable nor =
pass it=20
as a ref or out parameter. We are doing both in the above program, hence =
the=20
error. <SPAN style=3D"COLOR: blue"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN=20
style=3D"COLOR: red"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN style=3D"FONT-SIZE: 14pt; FONT-FAMILY: =
Tahoma">Jump=20
Statements</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=3DCbase><SPAN style=3D"COLOR: windowtext">Jump statements =
transfer control=20
to another part of our program. This transfer takes place come hell, =
hath or=20
fury. In other words, the transfer is unconditional. We have exactly =
five=20
conditional statements namely break, continue, goto, return and throw. =
The=20
statements that a jump takes you to, is called the target of the=20
jump.<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 break statement =
was touched=20
upon in the switch statement where it exits out of the case. It works in =
a=20
similar fashion for a while, for, foreach and do while. It simply exits =
out of=20
the looping construct it is befitted in. <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>break;<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 Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(5,1): error CS0139: No enclosing loop out of =
which to=20
break or continue<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">You cannot have a =
break statement=20
by itself. It has to be placed within a case or a loop=20
construct.<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>public static void Main() {<o:p></o:p></P>
<P class=3DCprg>int i =3D 1, j =3D 1;<o:p></o:p></P>
<P class=3DCprg>for ( i =3D 1; i<=3D 10 ; i++) {<o:p></o:p></P>
<P class=3DCprg>if ( i =3D=3D 3)<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>for ( j =3D 1; j <=3D 10; j++)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i + "." + j);<o:p></o:p></P>
<P class=3DCprg>if ( j =3D=3D 1)<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>System.Console.WriteLine(i + " " + j);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>1.1<o:p></o:p></P>
<P class=3DCoutput>2.1<o:p></o:p></P>
<P class=3DCoutput>3 1<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 number of breaks =
introduced=20
in the program must be equal to the loops statements. As all of us are =
very=20
particular about money and will never forget who we lent it to, =
similarly C# is=20
very finicky about breaks and hates it hanging around in the program =
code.=20
<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 inner break makes =
sure that=20
the inner for executes only once and not 10 times. It ends the loop each =
time=20
the value of j reaches 1. The outer break lets us prematurely exit the =
outer for=20
loop when the value of i is 3. As we love pairing up people, C# pairs up =
breaks=20
with loops.<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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -