break instrukcja (C++)

Instrukcja break kończy wykonywanie najbliższej otaczającej pętli lub instrukcji warunkowej, w której się pojawia. Jeśli po końcu przerwanej instrukcji występuje kolejna, sterowanie przechodzi do niej.

Składnia

break;

Uwagi

Instrukcja break jest używana z instrukcją warunkową switch i instrukcjami do, fori while loop.

switch W instrukcji instrukcja break powoduje, że program wykonuje następną instrukcję poza instrukcją switch . break Bez instrukcji każda instrukcja z dopasowanej case etykiety na końcu instrukcjiswitch, w tym klauzuladefault, jest wykonywana.

W pętlach instrukcja break kończy wykonywanie najbliższej otaczającej doinstrukcji , forlub while . Sterowanie przechodzi do instrukcji następującej po zakończonej, jeśli taka istnieje.

W instrukcjach break zagnieżdżonych instrukcja kończy tylko instrukcję do, for, switchlub while , która natychmiast go ujęła. Możesz użyć instrukcji return or goto do transferu kontroli z bardziej zagnieżdżonych struktur.

Przykład

Poniższy kod pokazuje, jak używać instrukcji break w for pętli.

#include <iostream>
using namespace std;

int main()
{
    // An example of a standard for loop
    for (int i = 1; i < 10; i++)
    {
        if (i == 4) {
            break;
        }
        cout << i << '\n';
    }

    // An example of a range-based for loop
int nums []{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    for (int i : nums) {
        if (i == 4) {
            break;
        }
        cout << i << '\n';
    }
}
1
2
3
1
2
3

Poniższy kod pokazuje, jak używać break w while pętli i do pętli.

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    while (i < 10) {
        if (i == 4) {
            break;
        }
        cout << i << '\n';
        i++;
    }

    i = 0;
    do {
        if (i == 4) {
            break;
        }
        cout << i << '\n';
        i++;
    } while (i < 10);
}
0
1
2
3
0
1
2
3

Poniższy kod pokazuje, jak używać break w instrukcji switch. Należy użyć break w każdym przypadku, jeśli chcesz obsługiwać poszczególne przypadki oddzielnie. Jeśli nie używasz breakmetody , wykonanie kodu przechodzi do następnego przypadku.

#include <iostream>
using namespace std;

enum Suit{ Diamonds, Hearts, Clubs, Spades };

int main() {

    Suit hand;
    . . .
    // Assume that some enum value is set for hand
    // In this example, each case is handled separately
    switch (hand)
    {
    case Diamonds:
        cout << "got Diamonds \n";
        break;
    case Hearts:
        cout << "got Hearts \n";
        break;
    case Clubs:
        cout << "got Clubs \n";
        break;
    case Spades:
        cout << "got Spades \n";
        break;
    default:
          cout << "didn't get card \n";
    }
    // In this example, Diamonds and Hearts are handled one way, and
    // Clubs, Spades, and the default value are handled another way
    switch (hand)
    {
    case Diamonds:
    case Hearts:
        cout << "got a red card \n";
        break;
    case Clubs:
    case Spades:
    default:
        cout << "didn't get a red card \n";
    }
}

Zobacz też

Instrukcje skoku
Słowa kluczowe
continue, instrukcja