📜  C#|跳转语句(中断,继续,转到,返回和抛出)

📅  最后修改于: 2021-05-29 18:18:15             🧑  作者: Mango

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

  • 休息
  • 继续
  • 返回

中断声明

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

流程图:

例子:

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

继续声明

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

例子:

// 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# 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

退货声明

该语句终止方法的执行,并将控件返回给调用方法。它返回一个可选值。如果方法的类型为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

抛出声明

它用于借助new关键字手动创建任何有效异常类的对象。有效异常必须从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