📜  C#– Break语句

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

在C#中,break语句用于在特定条件下终止循环(for,if,while等)或switch语句。如果可用,终止控件后的结果将传递到break语句之后的语句。如果break语句存在于嵌套循环中,则它将仅终止那些包含break语句的循环。

句法:

break;

流程图:

现在,我们将看到break语句的用法:

  1. 简单循环
  2. 嵌套循环
  3. 无限循环
  4. Switch-Case语句

1.简单循环:

在这里,我们将讨论在简单的for循环中使用break语句。如下例所示,将for循环编程为从0到20执行,但此示例的输出为“ 0 1 2 3 4 5 6”。因为在这里,当x的值等于7时,我们中断了循环。如果不使用break语句,则此循环将打印0….20数字。

C#
// C# program to illustrate the use of 
// break statement in loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Here, the break statement
    // terminates the loop when x = 7
    for(int x = 0; x <= 20; x++)
    {
        if (x == 7)
        {
            break;
        }
        Console.WriteLine(x);
    }
}
}


C#
// C# program to illustrate the use of 
// break statement in nested loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Outer Loop
    for(int x = 0; x < 4; x++)
    {  
        // Inner Loop
        for(int y = 1; y < 4; y++)
        {
            if (y > 2)
            {
                break;
            }
            Console.Write("#");
        }
        Console.Write("\n");
    }
}
}


C#
// C# program to illustrate 
// infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    // Creating infinite loop
    // using while loop
    while (true)
    {
        // This statement will be printed 
        // infinite times 
        Console.WriteLine("Hey GeeksforGeeks");
          x++;
    }
}
}


C#
// C# program to illustrate the use of
// break statement in the infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    while (true)
    {
        if (x == 7)
            break;
              
        Console.WriteLine("Hey GeeksforGeeks");
        x++;
    }
}
}


C#
// C# program to illustrate the use of 
// break statement in switch-case statement
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Enter the value
    Console.Write("Select option(1, 2, 3): ");
    string str = Console.ReadLine();
    int x = Int32.Parse(str);
      
    // Using break statement in the switch-case
    // statemets
     switch(x)
    {
        case 1:
            Console.WriteLine("You select group A");
            break;
        case 2:
            Console.WriteLine("You select group B");
            break;
        case 3:
            Console.WriteLine("You select group C");
            break;
        default:
            Console.WriteLine("Sorry, Not a valid selection!");
            break;
    }
}
}


输出:

0
1
2
3
4
5
6

2.嵌套循环:

我们还可以在嵌套循环中使用break语句。如果在最内层循环中使用break语句,则控制将仅从最内层循环中出来。让我们借助示例讨论在嵌套循环中使用break语句:

C#

// C# program to illustrate the use of 
// break statement in nested loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Outer Loop
    for(int x = 0; x < 4; x++)
    {  
        // Inner Loop
        for(int y = 1; y < 4; y++)
        {
            if (y > 2)
            {
                break;
            }
            Console.Write("#");
        }
        Console.Write("\n");
    }
}
}

输出:

##
##
##
##

在上面的示例中,内部循环被编程为执行4次迭代,但是当y的值大于2时,内部循环停止执行,因为我们使用break语句将内部循环的迭代次数限制为2。但是,外环不受影响。

3.无限循环:

我们还可以在无限循环中使用break语句终止无限循环的执行。让我们借助示例讨论无限循环中break语句的使用:

C#

// C# program to illustrate 
// infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    // Creating infinite loop
    // using while loop
    while (true)
    {
        // This statement will be printed 
        // infinite times 
        Console.WriteLine("Hey GeeksforGeeks");
          x++;
    }
}
}

在上面的示例中,循环终止所基于的循环条件始终为真。因此,循环将执行 无限次,或者我们可以说永不终止。因此,这里我们使用break语句在x的值等于7时终止循环

C#

// C# program to illustrate the use of
// break statement in the infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    while (true)
    {
        if (x == 7)
            break;
              
        Console.WriteLine("Hey GeeksforGeeks");
        x++;
    }
}
}

输出:

Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks

4.开关案例声明:

我们知道switch语句有一个缺点,即当找到匹配值时,它将执行所有语句,直到switch块结束。为了避免此类问题,我们在每种情况下都使用break语句。这样break语句终止了不匹配语句的switch语句的执行。如下例所示:

C#

// C# program to illustrate the use of 
// break statement in switch-case statement
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Enter the value
    Console.Write("Select option(1, 2, 3): ");
    string str = Console.ReadLine();
    int x = Int32.Parse(str);
      
    // Using break statement in the switch-case
    // statemets
     switch(x)
    {
        case 1:
            Console.WriteLine("You select group A");
            break;
        case 2:
            Console.WriteLine("You select group B");
            break;
        case 3:
            Console.WriteLine("You select group C");
            break;
        default:
            Console.WriteLine("Sorry, Not a valid selection!");
            break;
    }
}
}

输出:

Select option(1, 2, 3): 2
You select group B