Continue:

continue 语句将控制传递到封闭 、、或显示的 语句下一次迭代。

在本示例中,计数器最初是从 1 到 10 进行计数。  通过使用该表达式 (i < 9)一起的 continue 语句,在 continuefor 主体结束的语句跳过。

 class ContinueTest    {        static void Main()        {            for (int i = 1; i <= 10; i++)            {                if (i < 9)                {                                    continue;                }                Console.WriteLine(i);            }                        // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }        /*    Output:    9    10    */

备注:转自

**************************

While:

while 语句执行一个语句或语句块,直到指定的表达式计算为 false

C#

        class WhileTest     {            static void Main()         {                    int n = 1;                    while (n < 6)             {                Console.WriteLine("Current value of n is {0}", n);                n++;            }        }    }        /*        Output:        Current value of n is 1        Current value of n is 2        Current value of n is 3        Current value of n is 4        Current value of n is 5     */

C#

    class WhileTest2     {            static void Main()         {                    int n = 1;                     while (n++ < 6)             {                Console.WriteLine("Current value of n is {0}", n);            }        }    }    /*    Output:    Current value of n is 2    Current value of n is 3    Current value of n is 4    Current value of n is 5    Current value of n is 6    */

由于 while 表达式的测试在每次执行循环前发生,因此 while 循环执行零次或更多次。  这与执行一次或多次的 循环不同。

语句将控制权转移到 while 循环之外时,可以终止该循环。  若要将控制权传递给下一次迭代但不退出循环,请使用 语句。  请注意,在上面三个示例中,根据 int n 递增的位置的不同,输出也不同。  在下面的示例中不生成输出。

C#

    class WhileTest3    {        static void Main()         {                    int n = 5;                    while (++n < 6)             {                Console.WriteLine("Current value of n is {0}", n);            }        }    }

备注:转自

******************************************

break:

break 语句用于终止最近的封闭循环或它所在的 语句。  控制传递给终止语句后面的语句(如果有的话)。

在此示例中,条件语句包含一个应从 1 计数到 100 的计数器;但 break 语句在计数器计数到 4 后终止了循环。

C#

    class BreakTest    {        static void Main()        {            for (int i = 1; i <= 100; i++)            {                            if (i == 5)                {                                    break;                }                Console.WriteLine(i);            }                        // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    /*      Output:        1        2        3        4      */

在此示例中,break 语句用于中断内层嵌套循环,并将控制权返回给外层循环。

C#

    class BreakInNestedLoops    {        static void Main(string[] args)        {                    int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };                    char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };                    // Outer loop            for (int x = 0; x < numbers.Length; x++)            {                Console.WriteLine("num = {0}", numbers[x]);                // Inner loop                for (int y = 0; y < letters.Length; y++)                {                    if (y == x)                    {                        // Return control to outer loop                        break;                    }                    Console.Write(" {0} ", letters[y]);                }                Console.WriteLine();            }            // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    /*     * Output:        num = 0        num = 1         a        num = 2         a  b        num = 3         a  b  c        num = 4         a  b  c  d        num = 5         a  b  c  d  e        num = 6         a  b  c  d  e  f        num = 7         a  b  c  d  e  f  g        num = 8         a  b  c  d  e  f  g  h        num = 9         a  b  c  d  e  f  g  h  i     */

下面的示例演示 break 在 语句中的用法。

C#

    class Switch    {        static void Main()        {            Console.Write("Enter your selection (1, 2, or 3): ");            string s = Console.ReadLine();                        int n = Int32.Parse(s);                       switch (n)            {                            case 1:                    Console.WriteLine("Current value is {0}", 1);                                        break;                            case 2:                    Console.WriteLine("Current value is {0}", 2);                                        break;                            case 3:                    Console.WriteLine("Current value is {0}", 3);                                                            break;                            default:                    Console.WriteLine("Sorry, invalid selection.");                                        break;            }            // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    /*    Sample Input: 1         Sample Output:    Enter your selection (1, 2, or 3): 1    Current value is 1    */

如果输入了 4,则输出为:

Enter your selection (1, 2, or 3): 4  Sorry, invalid selection.

备注:转自

*******************************************

goto:

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

goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。

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

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

 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.");                        // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    /*    Sample Input:  2         Sample Output:    Coffee sizes: 1=Small 2=Medium 3=Large    Please enter your selection: 2    Please insert 50 cents.    Thank you for your business.    */

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

C#

    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.");                        // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    /*    Sample Input: 44         Sample Output    Enter the number to search for: 44    The number 44 is found.    End of search.    */

备注:转自

*************************************

Return:

return 语句终止它出现在其中的方法的执行并将控制返回给调用方法。  它还可以返回一个可选值。  如果方法为 void 类型,则可以省略 return 语句。

如果 return 语句位于 try 块中,则将在控制流返回到调用方法之前执行 finally 块(如果存在)。

在下面的示例中,方法 A() 以 值的形式返回变量 Area

 class ReturnTest    {            static double CalculateArea(int r)        {                    double area = r * r * Math.PI;                     return area;        }                static void Main()        {                    int radius = 5;                    double result = CalculateArea(radius);            Console.WriteLine("The area is {0:0.00}", result);                        // Keep the console open in debug mode.            Console.WriteLine("Press any key to exit.");            Console.ReadKey();        }    }    // Output: The area is 78.54

备注:摘自

*********************************

Throw:

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

引发的异常是一个对象,其类派生自 ,如以下示例所示。

class MyException : System.Exception {}  // ...  throw new MyException();

通常,throw 语句与 try-catchtry-finally 语句结合使用。可在 catch 块中使用 语句以重新引发已由 catch 块捕获的异常。在这种情况下, 语句不采用异常操作数。有关更多信息和示例,请参见 和。

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

  public class ThrowTest2    {            static int GetNumber(int index)        {                    int[] nums = { 300, 600, 900 };                    if (index > nums.Length)            {                            throw new IndexOutOfRangeException();            }                        return nums[index];         }                static void Main()         {                    int result = GetNumber(3);                    }    }    /*        Output:        The System.IndexOutOfRangeException exception occurs.    */

备注:摘自