goto statement
Aus cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Transfers Steuerung zu einem neuen Standort .
Original:
Transfers control to a new location.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Verwendet werden, wenn es anders nicht möglich ist, die Kontrolle an der gewünschten Stelle mit herkömmlichen Konstruktionen zu übertragen .
Original:
Used when it is otherwise impossible to transfer control to the desired location using conventional constructs.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Inhaltsverzeichnis |
[Bearbeiten] Syntax
goto label
|
|||||||||
[Bearbeiten] Erklärung
ist. Die goto-Anweisung überträgt die Steuerung an die Stelle von label angegeben. Die goto-Anweisung muss sich in die gleiche Funktion wie der label Sie verweisen wollen. Wenn goto Anweisung überträgt rückwärts steuern, werden alle Objekte, die noch nicht an der label initialisiert zerstört. Es ist illegal, die Kontrolle nach vorne zu übertragen, wenn dies Initialisierung eines Objekts .
Original:
The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring. If goto statement transfers control backwards, all objects that are not yet initialized at the label are destructed. It is illegal to transfer control forwards if doing so would skip initialization of an object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Keywords
[Bearbeiten] Beispiel
#include <iostream> struct Object { ~Object() { std::cout << "d"; } }; int main() { int a = 10; //loop using goto label: Object obj; std::cout << a << " "; a = a - 2; if (a != 0) { goto label; //causes obj to be destructed } std::cout << '\n'; //get out of multi-level loop easily for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { std::cout << "(" << x << ";" << y << ") " << '\n'; if (x + y >= 3) { goto endloop; } } } endloop: std::cout << '\n'; return 0; //causes obj to be destructed }
Output:
10 d8 d6 d4 d2 (0;0) (0;1) (0;2) (1;0) (1;1) (1;2) d