📜  C＃– Break语句

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

``````break;
``````

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

1.简单循环：

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): ");
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.嵌套循环：

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");
}
}
}
``````

``````##
##
##
##
``````

3.无限循环：

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++;
}
}
}
``````

``````Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
``````

4.开关案例声明：

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): ");
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
``````