700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 黑马程序员--c#中break_continue_return__goto_throw的区别

黑马程序员--c#中break_continue_return__goto_throw的区别

时间:2019-11-04 12:17:58

相关推荐

黑马程序员--c#中break_continue_return__goto_throw的区别

------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------

c#中break,continue,return,,goto,throw的区别

break语句用于终止最近的封闭循环或它所在的switch语句。

控制传递给终止语句后面的语句(如果有的话)。

continue语句将控制权传递给它所在的封闭迭代语句的下一次迭代。

goto语句将程序控制直接传递给标记语句。

goto的一个通常用法是将控制传递给

特定的switch-case标签

或switch语句中的默认标签。

goto语句还用于跳出深嵌套循环。

return语句终止它出现在其中的方法的执行并将控制返回给调用方法。

它还可以返回一个可选值。

如果方法为void类型,则可以省略return语句。

throw语句用于发出在程序执行期间出现反常情况(异常)的信号。

通常throw语句与try-catch或try-finally语句一起使用。

当引发异常时,程序查找处理此异常的catch语句。

也可以用throw语句重新引发已捕获的异常。

-------------------------------------------------------------

----------

break示例

----------

在此例中,条件语句包含一个应该从1计数到100的计数器;

但break语句在计数达到4后终止循环。

//statements_break.cs

usingSystem;

classBreakTest

{

staticvoidMain()

{

for(inti=1;i<=100;i++)

{

if(i==5)

{

break;

}

Console.WriteLine(i);

}

}

}

输出

1

2

3

4

--------------

continue示例

--------------

在此示例中,计数器最初是从1到10进行计数,

但通过将continue语句与表达式(i<9)一起使用,

跳过了continue与for循环体末尾之间的语句。

//statements_continue.cs

usingSystem;

classContinueTest

{

staticvoidMain()

{

for(inti=1;i<=10;i++)

{

if(i<9)

{

continue;

}

Console.WriteLine(i);

}

}

}

输出

9

10

----------

goto示例1

----------

下面的示例演示了goto在switch语句中的使用。

//statements_goto_switch.cs

usingSystem;

classSwitchTest

{

staticvoidMain()

{

Console.WriteLine("Coffeesizes:1=Small2=Medium3=Large");

Console.Write("Pleaseenteryourselection:");

strings=Console.ReadLine();

intn=int.Parse(s);

intcost=0;

switch(n)

{

case1:

cost+=25;

break;

case2:

cost+=25;

gotocase1;

case3:

cost+=50;

gotocase1;

default:

Console.WriteLine("Invalidselection.");

break;

}

if(cost!=0)

{

Console.WriteLine("Pleaseinsert{0}cents.",cost);

}

Console.WriteLine("Thankyouforyourbusiness.");

}

}

输入

2

示例输出

Coffeesizes:1=Small2=Medium3=Large

Pleaseenteryourselection:2

Pleaseinsert50cents.

Thankyouforyourbusiness.

----------

goto示例2

----------

下面的示例演示了使用goto跳出嵌套循环。

//statements_goto.cs

//Nestedsearchloops

usingSystem;

publicclassGotoTest1

{

staticvoidMain()

{

intx=200,y=4;

intcount=0;

string[,]array=newstring[x,y];

//Initializethearray:

for(inti=0;i<x;i++)

for(intj=0;j<y;j++)

array[i,j]=(++count).ToString();

//Readinput:

Console.Write("Enterthenumbertosearchfor:");

//Inputastring:

stringmyNumber=Console.ReadLine();

//Search:

for(inti=0;i<x;i++)

{

for(intj=0;j<y;j++)

{

if(array[i,j].Equals(myNumber))

{

gotoFound;

}

}

}

Console.WriteLine("Thenumber{0}wasnotfound.",myNumber);

gotoFinish;

Found:

Console.WriteLine("Thenumber{0}isfound.",myNumber);

Finish:

Console.WriteLine("Endofsearch.");

}

}

输入

44

示例输出

Enterthenumbertosearchfor:44

Thenumber44isfound.

Endofsearch.

----------

throw示例

----------

此例演示如何使用throw语句引发异常。

//throwexample

usingSystem;

publicclassThrowTest

{

staticvoidMain()

{

strings=null;

if(s==null)

{

thrownewArgumentNullException();

}

Console.Write("Thestringsisnull");//notexecuted

}

}

------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。