Espaces de noms
Variantes
Affichages
Actions

goto statement

De cppreference.com
< cpp‎ | language

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
instruction goto
instruction return
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 
Transferts de contrôle vers un nouvel emplacement .
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.
Utilisé quand il en est autrement impossible de transférer le contrôle à l'endroit désiré en utilisant des constructions conventionnelles .
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.

Sommaire

[modifier] Syntaxe

goto label

[modifier] Explication

L'instruction goto transfère le contrôle à l'emplacement spécifié par label. L'instruction goto doit être en la même fonction que la label il se réfère. Si les transferts instruction goto contrôle en arrière, tous les objets qui ne sont pas encore initialisées à la label sont détruites. Il est illégal de transférer vers l'avant contrôle si cela sauter l'initialisation d'un objet .
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.

[modifier] Mots-clés

goto

[modifier] Exemple

#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
}

Résultat :

10 d8 d6 d4 d2
(0;0) (0;1) (0;2) (1;0) (1;1) (1;2)
d