C#|跳转语句(Break, Continue, Goto, Return和Throw)

2021年5月9日16:19:57 发表评论 828 次浏览

C#中, 由于执行程序时某些指定的代码, 跳转语句用于将控制从程序中的一点转移到另一点。跳转语句中有五个关键字:

Break

Continue

Goto

return

throw

break声明

break语句用于终止其所在的循环或语句。此后, 控件将传递到break语句之后出现的语句(如果有)。如果break语句存在于嵌套循环中, 则它仅终止那些包含break语句的循环。

流程图:

C#|跳转语句(Break, Continue, Goto, Return和Throw)1

例子:

//C# program to illustrate the
//use of break statement
using System;
  
class Geeks {
  
     //Main Method
     static public void Main()
     {
  
         //lsbin is printed only 2 times
         //because of break statement
         for ( int i = 1; i <4; i++) 
         {
             if (i == 3)
                 break ;
  
             Console.WriteLine( "lsbin" );
         }
     }
}

输出如下:

lsbin
lsbin

Continue声明

该语句用于在特定条件下跳过循环的执行部分。之后, 它将控制转移到循环的开始。基本上, 它跳过以下语句, 并继续循环的下一个迭代。

C#|跳转语句(Break, Continue, Goto, Return和Throw)2

例子:

//C# program to illustrate the
//use of continue statement
using System;
  
class Geeks {
  
     //Main Method
     public static void Main()
     {
          
         //This will skip 4 to print
         for ( int i = 1; i <= 10; i++) {
  
             //if the value of i becomes 4 then 
             //it will skip 4 and send the 
             //transfer to the for loop and 
             //continue with 5 
             if (i == 4)
                 continue ;
  
             Console.WriteLine(i);
         }
     }
}

输出如下:

1
2
3
5
6
7
8
9
10

goto声明

该语句用于将控制权转移到程序中带标签的语句。标签是有效的标识符, 位于控件从其中转移的语句之前。

C#|跳转语句(Break, Continue, Goto, Return和Throw)3

例子:

//C# program to illustrate the
//use of goto statement
using System;
  
class Geeks {
  
     //Main Method
     static public void Main()
     {
         int number = 20;
         switch (number) {
  
         case 5:
             Console.WriteLine( "case 5" );
             break ;
         case 10:
             Console.WriteLine( "case 10" );
             break ;
         case 20:
             Console.WriteLine( "case 20" );
  
             //goto statement transfer 
             //the control to case 5
             goto case 5;
  
         default :
             Console.WriteLine( "No match found" );
             break ;
         }
     }
}

输出如下:

case 20
case 5

return声明

该语句终止方法的执行, 并将控件返回给调用方法。它返回一个可选值。如果方法的类型为void, 则可以排除return语句。

例子:

//C# program to illustrate the
//use of return statement
using System;
  
class Geeks {
  
     //creating simple addition function
     static int Addition( int a)
     {
  
         //add two value and
         //return the result of addition
         int add = a + a;
         
         //using return statement
         return add;
     }
  
     //Main Method
     static public void Main()
     {
         int number = 2;
  
         //calling addition function
         int result = Addition(number);
         Console.WriteLine( "The addition is {0}" , result);
     }
}

输出如下:

The addition is 4

throw声明

它用于借助以下命令创建任何有效异常类的对象新手动设置关键字。有效的异常必须从Exception类派生。

例子:

//C# Program to illustrate the use 
//of throw keyword
using System;
  
class Geeks {
      
      //takinmg null in the string
      static string sub = null ;
          
     //method to display subject name    
     static void displaysubject( string sub1)
     {
         if (sub1  == null )
             throw new NullReferenceException( "Exception Message" );
              
     }
  
//Main Method    
static void Main( string [] args)
{
      
     //using try catch block to 
     //handle the Exception
     try
     {
          
         //calling the static method
         displaysubject(sub);
     }
      
     catch (Exception exp)
     {
         Console.WriteLine(exp.Message );
     }                     
}
  
}

输出如下:

Exception Message

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: