zoukankan      html  css  js  c++  java
  • C#中break,continue,return,,goto,throw的区别(转)

    break     

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

            /// <summary>
            /// break 示例
            /// 在此例中,条件语句包含一个应该从 1 计数到 100 的计数器
            /// 但 break 语句在计数达到 4 后终止循环。
            /// </summary>
            static void Main()
            {
                for (int i = 1; i <= 100; i++)
                {
                    if (i == 5)
                    {
                        break;
                    }
                    Console.WriteLine(i);
                }
            }

    continue 

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

            /// <summary>
            /// continue 示例
            /// 在此示例中,计数器最初是从 1 到 10 进行计数 
            // 但通过将 continue 语句与表达式 (i< 9) 一起使用
            /// 跳过了 continue 与 for 循环体末尾之间的语句。
            /// </summary>
            static void Main()
            {
                for (int i = 1; i <= 10; i++)
                {
                    if (i < 9)
                    {
                        continue;
                    }
                    Console.WriteLine(i);
                }
            }

    goto      

    语句将程序控制直接传递给标记语句。goto的一个通常用法是将控制传递给特定的switch-case 标签或switch 语句中的默认标签。 goto语句还用于跳出深嵌套循环。

            /// <summary>
            /// goto 示例
            /// 输入 2 
            /// 示例输出
            /// Coffee sizes: 1=Small 2=Medium 3=Large
            /// Please enter your selection: 2 
            /// Please insert 50 cents. 
            /// Thank you for your business.
            /// </summary>
            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.");
            }
            /// <summary>
            /// goto 示例2
            /// 下面的示例演示了使用 goto 跳出嵌套循环
            /// 输入 44 示例输出 
            /// Enter the number to search for: 44
            /// The number 44 is found.
            /// End of search.
            /// </summary>
            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.");  
            }

    return   

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

     

    throw    

    语句用于发出在程序执行期间出现反常情况(异常)的信号。通常throw语句与try-catch或try-finally 语句一起使用。当引发异常时,程序查找处理此异常的catch 语句。也可以用

    throw 语句重新引发已捕获的异常。

     

            /// <summary>
            /// thow 示例
            /// </summary>
            static void Main()
            {
                string s = null;
                if (s == null)
                {
                    throw new ArgumentNullException();
                }
                Console.Write("The string s is null");
                // not executed   
            }
  • 相关阅读:
    体验ASP.NET 2.0中的BuildProvider(转载)
    为什么要用非关系数据库?
    Inside ASP.NET 2.0即时编译系统(转载)
    文本信息检索(维基百科)
    通用数据压缩算法简介
    在HttpModule中使用gzip,deflate协议对aspx页面进行压缩
    NoSQL非关系型数据库
    fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786)
    C++中的变量 Variables in C++
    Visual C++, pow(),error C2065: 'pow' : undeclared identifier
  • 原文地址:https://www.cnblogs.com/zhizhuo-1991/p/5591662.html
Copyright © 2011-2022 走看看