📜  C / C++中的Break语句

📅  最后修改于: 2021-05-25 23:22:51             🧑  作者: Mango

C或C++中的break是一个循环控制语句，用于终止循环。一旦从循环内遇到break语句，循环迭代便在那里停止，控制权立即从循环返回到循环后的第一个语句。

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

1. 简单循环
2. 嵌套循环
3. 无限循环

1. 简单循环：考虑我们要在数组中搜索元素的情况。为此，请使用循环从第一个索引开始遍历数组，并将数组元素与给定键进行比较。
以下是此想法的实现：
##### C
``````// C program to illustrate
// Linear Search

#include

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d", (i + 1));
}
}
}

int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };

// no of elements
int n = 6;

// key to be searched
int key = 3;

// Calling function to find the key
findElement(arr, n, key);

return 0;
}``````

##### C++
``````// CPP program to illustrate
// Linear Search
#include
using namespace std;

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
cout << "Element found at position: " << (i + 1);
}
}
}

// Driver program to test above function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6; // no of elements
int key = 3; // key to be searched

// Calling function to find the key
findElement(arr, n, key);

return 0;
}``````

##### C
``````// C program to illustrate
// using break statement
// in Linear Search
#include

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d", (i + 1));

// using break to terminate loop execution
break;
}
}
}

int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };

// no of elements
int n = 6;

// key to be searched
int key = 3;

// Calling function to find the key
findElement(arr, n, key);

return 0;
}``````

##### C++
``````// CPP program to illustrate
// using break statement
// in Linear Search
#include
using namespace std;

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
cout << "Element found at position: " << (i + 1);

// using break to terminate loop execution
break;
}
}
}

// Driver program to test above function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6; // no of elements
int key = 3; // key to be searched

// Calling function to find the key
findElement(arr, n, key);

return 0;
}``````

##### C
``````// C program to illustrate
// using break statement
// in Nested loops
#include

int main() {
// nested for loops with break statement
// at inner loop
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j > 3)
break;
else
printf("*");
}
printf("\n");
}

return 0;
}``````

##### C++
``````// CPP program to illustrate
// using break statement
// in Nested loops
#include
using namespace std;

int main()
{

// nested for loops with break statement
// at inner loop
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j > 3)
break;
else
cout << "*";
}
cout << endl;
}

return 0;
}``````

##### C
``````// C program to illustrate
// using break statement
// in Infinite loops
#include

int main() {
// loop initialization expression
int i = 0;

// infinite while loop
while (1) {
printf("%d ", i);
i++;
}

return 0;
}``````

##### C++
``````// CPP program to illustrate
// using break statement
// in Infinite loops
#include
using namespace std;

int main()
{
// loop initialization expression
int i = 0;

// infinite while loop
while (1) {
cout << i << " ";
i++;
}

return 0;
}``````

##### C
``````// C program to illustrate
// using break statement
// in Infinite loops
#include

int main() {
// loop initialization expression
int i = 1;

// infinite while loop
while (1) {
if (i > 10)
break;

printf("%d ", i);
i++;
}

return 0;
}``````

##### C++
``````// CPP program to illustrate
// using break statement
// in Infinite loops
#include
using namespace std;

int main()
{
// loop initialization expression
int i = 1;

// infinite while loop
while (1) {
if (i > 10)
break;

cout << i << " ";
i++;
}

return 0;
}
Output:1 2 3 4 5 6 7 8 9 10
The above code restricts the number of loop iterations to 10.Apart from this, break can be used in Switch case statements too.Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.``````

输出：

``````Element found at index: 3
``````

上面的代码运行正常，没有错误。但是上面的代码效率不高。上面的代码甚至在找到元素之后也完成了所有迭代。假设数组中有1000个元素，并且要搜索的键位于第一个位置，那么上述方法将执行999次无用且无用的迭代。
为了避免这些无用的迭代，我们可以在程序中使用break语句。遇到break语句后，满足条件后，将立即返回循环中的控件。因此将使用带有if条件的break语句，该条件将键与数组元素进行比较，如下所示：

## C

``````// C program to illustrate
// using break statement
// in Linear Search
#include

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d", (i + 1));

// using break to terminate loop execution
break;
}
}
}

int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };

// no of elements
int n = 6;

// key to be searched
int key = 3;

// Calling function to find the key
findElement(arr, n, key);

return 0;
}
``````

## C++

``````// CPP program to illustrate
// using break statement
// in Linear Search
#include
using namespace std;

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
cout << "Element found at position: " << (i + 1);

// using break to terminate loop execution
break;
}
}
}

// Driver program to test above function
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6; // no of elements
int key = 3; // key to be searched

// Calling function to find the key
findElement(arr, n, key);

return 0;
}
``````

输出：

``````Element found at position: 3
``````
2. 嵌套循环：在嵌套循环中，我们还可以使用break语句。如果在最里面的循环中使用了break语句。该控件将仅从最内部的循环中出来。下面是使用带有嵌套循环的break的示例：

## C

``````// C program to illustrate
// using break statement
// in Nested loops
#include

int main() {
// nested for loops with break statement
// at inner loop
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j > 3)
break;
else
printf("*");
}
printf("\n");
}

return 0;
}
``````

## C++

``````// CPP program to illustrate
// using break statement
// in Nested loops
#include
using namespace std;

int main()
{

// nested for loops with break statement
// at inner loop
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j > 3)
break;
else
cout << "*";
}
cout << endl;
}

return 0;
}
``````

输出：

``````***
***
***
***
***
``````

在上面的代码中，我们可以清楚地看到将内部循环编程为执行10次迭代。但是，一旦j的值变得大于3，内部循环便停止执行，这将内部循环的迭代次数限制为仅3次迭代。但是，外循环的迭代不受影响。
因此，中断仅适用于存在中断的循环。

3. 无限循环：break语句可以包含在具有条件的无限循环中，以终止无限循环的执行。
考虑下面的无限循环：

## C

``````// C program to illustrate
// using break statement
// in Infinite loops
#include

int main() {
// loop initialization expression
int i = 0;

// infinite while loop
while (1) {
printf("%d ", i);
i++;
}

return 0;
}
``````

## C++

``````// CPP program to illustrate
// using break statement
// in Infinite loops
#include
using namespace std;

int main()
{
// loop initialization expression
int i = 0;

// infinite while loop
while (1) {
cout << i << " ";
i++;
}

return 0;
}
``````

注意：请不要在编译器中运行上述程序，因为它是一个无限循环，因此您可能必须强行退出编译器以终止该程序。

在上述程序中，循环终止所依据的循环条件始终为真。因此，循环执行了无数次。我们可以使用break语句来更正此问题，如下所示：

## C

``````// C program to illustrate
// using break statement
// in Infinite loops
#include

int main() {
// loop initialization expression
int i = 1;

// infinite while loop
while (1) {
if (i > 10)
break;

printf("%d ", i);
i++;
}

return 0;
}
``````

## C++

``````// CPP program to illustrate
// using break statement
// in Infinite loops
#include
using namespace std;

int main()
{
// loop initialization expression
int i = 1;

// infinite while loop
while (1) {
if (i > 10)
break;

cout << i << " ";
i++;
}

return 0;
}
``````

输出：

``````1 2 3 4 5 6 7 8 9 10
``````

上面的代码将循环迭代次数限制为10。

除此之外，break也可以在Switch的case语句中使用。

想要从精选的最佳视频中学习和练习问题，请查看《基础知识到高级C的C基础课程》。