`
net_liu
  • 浏览: 231332 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C# 跳转语句(break,continue,goto,return,throw)

阅读更多

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
    }
}

<script type="text/javascript"></script>

分享到:
评论

相关推荐

    C#的关键字大全

    * 跳转语句:break、continue、default、goto、return、yield * 异常处理语句:throw、try-catch、try-finally、try-catch-finally * 检查和未检查:checked、unchecked * fixed 语句:fixed * lock 语句:lock ...

    C#语言规范5.0中文版.pdf

    此外,控制流语句还包括break、continue、goto、throw和return,这些语句提供了在程序执行中进行更细粒度的控制。 C#语言规范还包括异常处理的详细描述,例如try-catch、try-finally和try-catch-finally块,它们...

    C#中的关键字

    * goto:跳转到标签。 * if:定义if语句。 * return:返回方法的值。 * throw:抛出异常。 * try:定义try-catch语句。 * while:定义while循环语句。 五、其他关键字 * as:转换对象的类型。 * is:检查对象的类型。 * ...

    c# 关键字 微软官方文档

    - 跳转语句关键字:包括 break、continue、return、throw、goto 等。 - 异常处理语句关键字:如 try、catch、finally、throw、throw new Exception() 等。 - checked 和 unchecked 语句关键字:用于整数算术运算...

    c#知识点对初学者很有帮助

    5. **表达式和控制流**:包括条件分支(if/else、switch)、循环(for、while、do/while)、跳转语句(goto、break、continue、return、throw)以及变量的作用域。理解这些可以帮助你控制程序流程,根据条件执行不同...

    C#常用关键字及含义.doc

    * break:用于从 loop 或 switch 语句中退出的跳转语句。 * case:指定在 switch 语句中的一个标签。 * catch:定义一个代码块,在特定类型异常抛出时,执行块内代码。 * continue:用于返回循环顶部的跳转语句。 * ...

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

    14. **语句和控制流**:C#中的语句控制程序流程,如条件语句(if、switch)、循环(while、for、foreach)、跳转语句(break、continue、goto、return、throw)以及异常处理(try-catch)。 15. **名称空间和类的...

    微软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#语言参考C#语言参考

    - **跳转语句**:`break`、`continue`、`goto`、`return`和`throw`。 8. **名称空间** - **命名空间**:组织代码的逻辑单元,包含类型声明和使用指示。 - **类和结构**:定义对象的蓝图,包含成员如字段、方法、...

    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#语法全解

    - **跳转语句**:break、continue、return和goto用于控制程序流程。 3. **函数与方法** - **函数定义**:C#中的函数使用关键字`void`或指定返回类型定义,参数列表放在圆括号内。 - **方法重载**:同名方法可以...

    c#学习笔记.txt

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

    cs代码-goto 语句

    在C#(通常简称为CS)编程语言中,`goto`语句是一种跳转语句,它允许程序在执行过程中不按照正常的顺序进行,而是直接跳转到代码中的某个标记位置继续执行。虽然`goto`在某些场景下可以提供便利,但它也被认为是一种...

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

    3.4.1 throw语句 63 3.4.2 try-catch语句 64 3.4.3 try-catch-finally语句 65 3.5 本章小结 66 第4章 数组 67 4.1 数组简介 68 4.1.1 数组的概述 68 4.1.2 数组的应用 68 4.2 静态数组 69 4.2.1...

    C#关键字列表

    - **goto**:无条件跳转到指定的标记位置。 - **return**:返回函数的调用处,可以带有返回值。 ##### 4. 类型与命名空间 - **class**:定义类的关键字。 - **struct**:定义结构体的关键字,与类类似但默认为值...

    Ch04程序流程和异常处理.pptx

    **跳转语句**如`goto`、`break`、`continue`、`return`和`throw`在特定情况下改变程序的执行流程。`goto`用于无条件跳转,但在现代编程实践中不建议使用。`break`用于退出当前循环,`continue`则跳过当前循环迭代的...

    C#帮助文档.doc

    `unchecked`, `class`, `if`, `readonly`, `Unsafe`, `const`, `implicit`, `ref`, `Ushort`, `continue`, `in`, `return`, `using`, `decimal`, `ints`, `sbyte`, `Value`, `default`, `interface`, `Sealed`, `...

Global site tag (gtag.js) - Google Analytics