`
yiyeqinghuasoon
  • 浏览: 643514 次
文章分类
社区版块
存档分类
最新评论

[引]C#参考跳转语句(break,continue,goto,return,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
using System;
class BreakTest
{
static void Main()
{
for (int i = 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
using System;
class ContinueTest
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
{
continue;
}
Console.WriteLine(i);
}
}
}

输出
9
10

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

// statements_goto_switch.cs
using System;
class SwitchTest
{
static 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 (n)
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
default:
Console.WriteLine("Invalid selection.");
break;
}
if (cost != 0)
{
Console.WriteLine("Please insert {0} cents.", cost);
}
Console.WriteLine("Thank you for your business.");
}
}

输入
2
示例输出
Coffee sizes: 1=Small 2=Medium 3=Large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.


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

// statements_goto.cs
// Nested search loops
using System;
public class GotoTest1
{
static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];

// Initialize the array:
for (int i = 0; i < x; i++)

for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();

// 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 (array[i, j].Equals(myNumber))
{
goto 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: 44
The number 44 is found.
End of search.

----------
throw 示例
----------
此例演示如何使用 throw 语句引发异常。
// throw example
using System;
public class ThrowTest
{
static void Main()
{
string s = null;

if (s == null)
{
throw new ArgumentNullException();
}

Console.Write("The string s is null"); // not executed
}
}

分享到:
评论

相关推荐

    微软 C#语言参考 CHM格式

    8.9.2 continue语句... 155 8.9.3 goto语句... 155 8.9.4 return语句... 156 8.9.5 throw语句... 156 8.10 try语句... 157 8.11 checked和unchecked语句... 159 8.12 lock语句... 159 9. 名称空间... 161 9.1 编译...

    C#语言参考C#语言参考

    8.9.2 continue语句 155 8.9.3 goto语句 155 8.9.4 return语句 156 8.9.5 throw语句 156 8.10 try语句 157 8.11 checked和unchecked语句 159 8.12 lock语句 159 9. 名称空间 161 9.1 编译单元 161 9.2 名称空间声明 ...

    微软C#语言规范,C#语言教程中文版

    5.3.3.10 break、continue 和 goto 语句 100 5.3.3.11 throw 语句 100 5.3.3.12 return 语句 100 5.3.3.13 try-catch 语句 100 5.3.3.14 try-finally 语句 100 5.3.3.15 try-catch-finally 语句 101 5.3.3.16 ...

    C#语言规范(2.0,3.0,4.0合集)

    5.3.3.10 break、continue 和 goto 语句 100 5.3.3.11 throw 语句 100 5.3.3.12 return 语句 100 5.3.3.13 try-catch 语句 100 5.3.3.14 try-finally 语句 100 5.3.3.15 try-catch-finally 语句 101 5.3.3.16 ...

    C#教程(语言规范)

    5.3.3.10 break、continue 和 goto 语句 91 5.3.3.11 throw 语句 91 5.3.3.12 return 语句 91 5.3.3.13 try-catch 语句 ... 92 5.3.3.14 try-finally 语句 ... 92 5.3.3.15 try-catch-finally 语句 92 5.3....

    c#学习笔记.txt

    跳转语句break, continue, default, goto, return 异常处理语句throw, try-catch, try-finally Checked 和 Uncheckedchecked, unchecked fixed 语句Fixed lock 语句Lock (1) foreach 语句为数组或对象集合中的每个...

    C#5.0本质论第四版(因文件较大传的是百度网盘地址)

    16.5.6 将yield return语句放到循环中 465 16.5.7 取消更多的迭代:yield break 467 16.5.8 在单个类中创建多个迭代器 469 16.5.9 yield语句的要求 470 16.6 小结 470 第17章 反射、特性和动态...

    C#语言参考,微软的基础教程

    8.9.2 continue语句 79 8.9.3 goto语句 79 8.9.4 return语句 79 8.9.5 throw语句 79 8.10 try语句 79 8.11 checked和unchecked语句 79 8.12 lock语句 79 9. 名称空间 79 9.1 编译单元 79 9.2 名称空间声明 79 9.3 ...

    Visual C# 2005程序设计自学手册 随书源码第一部分(共三部)

    3.3.2 使用continue语句的实现程序跳转 60 3.3.3 使用goto语句实现程序跳转 60 3.3.4 使用return语句实现程序跳转 62 3.4 异常处理语句 63 3.4.1 throw语句 63 3.4.2 try-catch语句 64 3.4.3 try-catch...

    C#全能速查宝典

    1.3.7 goto语句——跳转到标签 34 1.3.8 if…else语句——条件判断语句 36 1.3.9 return语句——返回 38 1.3.10 switch case语句——条件判断语句 39 1.3.11 throw语句——显式引发异常 40 1.3.12 try…catch…...

    C#语言规范(4.0版本)

    5.3.3.10 break、continue 和 goto 语句 100 5.3.3.11 throw 语句 100 5.3.3.12 return 语句 100 5.3.3.13 try-catch 语句 100 5.3.3.14 try-finally 语句 100 5.3.3.15 try-catch-finally 语句 101 5.3.3.16 ...

    C# for CSDN 乱七八糟的看不懂

    C#关键字 关键字 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach ...

    C#语言规范4.0

    5.3.3.10 break、continue 和 goto 语句 100 5.3.3.11 throw 语句 100 5.3.3.12 return 语句 100 5.3.3.13 try-catch 语句 100 5.3.3.14 try-finally 语句 100 5.3.3.15 try-catch-finally 语句 101 5.3.3.16 ...

    C#_语言规范_4.0_中文版

    5.3.3.10 break、continue 和 goto 语句 100 5.3.3.11 throw 语句 100 5.3.3.12 return 语句 100 5.3.3.13 try-catch 语句 100 5.3.3.14 try-finally 语句 100 5.3.3.15 try-catch-finally 语句 101 5.3.3.16 ...

    C# 语言规格说明(English Edition第五版)

    5.3.3.10 Break, continue, and goto statements 100 5.3.3.11 Throw statements 100 5.3.3.12 Return statements 100 5.3.3.13 Try-catch statements 100 5.3.3.14 Try-finally statements 101 5.3.3.15 Try-catch-...

    c#3.0语言规范高清PDF

    1.5 语句 ............................................................................................................................................................. 8 1.6 类和对象 .....................

Global site tag (gtag.js) - Google Analytics