c#中break,continue,return,,goto,throw的区别-_第1页
c#中break,continue,return,,goto,throw的区别-_第2页
c#中break,continue,return,,goto,throw的区别-_第3页
c#中break,continue,return,,goto,throw的区别-_第4页
c#中break,continue,return,,goto,throw的区别-_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c#中break,continue,return,goto,throw的区别(摘抄2010-08-18 16:17break 语句用于终止最近的封闭循环或它所在的switch 语句。控制传递给终止语句后面的语句(如果有的话。continue 语句将控制权传递给它所在的封闭迭代语句的下一次迭代。goto 语句将程序控制直接传递给标记语句。goto 的一个通常用法是将控制传递给特定的switch-case 标签或switch 语句中的默认标签。goto 语句还用于跳出深嵌套循环。return 语句终止它出现在其中的方法的执行并将控制返回给调用方法。它还可以返回一个可选值。如果方法为void 类型,

2、则可以省略return 语句。throw 语句用于发出在程序执行期间出现反常情况(异常的信号。通常throw 语句与try-catch 或try-finally 语句一起使用。当引发异常时,程序查找处理此异常的catch 语句。也可以用throw 语句重新引发已捕获的异常。-break 示例-在此例中,条件语句包含一个应该从 1 计数到100 的计数器;但break 语句在计数达到 4 后终止循环。/ statements_break.csusing System;class BreakTeststatic void Main(for (int i = 1; i = 100; i+if (i

3、= 5break;Console.WriteLine(i;输出1234-continue 示例-在此示例中,计数器最初是从 1 到10 进行计数,但通过将continue 语句与表达式(i 9 一起使用,跳过了continue 与for 循环体末尾之间的语句。/ statements_continue.csusing System;class ContinueTeststatic void Main(for (int i = 1; i = 10; i+if (i 9continue;Console.WriteLine(i;输出910-goto 示例1-下面的示例演示了goto 在switch

4、语句中的使用。/ statements_goto_switch.csusing System;class SwitchTeststatic void Main(Console.WriteLine(Coffee sizes: 1=Small 2=Medium 3=Large;Console.Write(Please enter your selection: ;string s = Console.ReadLine(;int n = int.Parse(s;int cost = 0;switch (ncase 1:cost += 25;break;case 2:cost += 25;goto c

5、ase 1;case 3:cost += 50;goto case 1;default:Console.WriteLine(Invalid selection.;break;if (cost != 0Console.WriteLine(Please insert 0 cents., cost;Console.WriteLine(Thank you for your business.;输入2示例输出Coffee sizes: 1=Small 2=Medium 3=LargePlease enter your selection: 2Please insert 50 cents.Thank yo

6、u for your business.-goto 示例2-下面的示例演示了使用goto 跳出嵌套循环。/ statements_goto.cs/ Nested search loopsusing System;public class GotoTest1static void Main(int x = 200, y = 4;int count = 0;string, array = new stringx, y;/ Initialize the array:for (int i = 0; i x; i+for (int j = 0; j y; j+arrayi, j = (+count.To

7、String(;/ Read input:Console.Write(Enter the number to search for: ;/ Input a string:string myNumber = Console.ReadLine(;/ Search:for (int i = 0; i x; i+for (int j = 0; j y; j+if (arrayi, j.Equals(myNumbergoto Found;Console.WriteLine(The number 0 was not found., myNumber;goto Finish;Found:Console.WriteLine(The number 0 is found., myNumber;Finish:Console.WriteLine(End of search.;输入44示例输出Enter the number to search for: 44The number 44 is found.End of search.-throw 示例-此例演示如何使用throw 语句引发异常。/ throw exampleusing System;public class ThrowTeststatic

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论