Partager via


Labeled Statements

Labels are used to transfer program control directly to the specified statement.

identifier :  statement
case constant-expression :  statement
default :  statement

The scope of a label is the entire function in which it is declared.

Remarks

There are three types of labeled statements. All use a colon to separate some type of label from the statement. The case and default labels are specific to case statements. See Using Labels with the goto Statement and Using Labels in the case Statement.

#include <iostream> 
using namespace std; 

void test_label(int x) {

    if (x == 1){
        goto label1;
    }
    goto label2;

label1:
    cout << "in label1" << endl;
    return;

label2:
    cout << "in label2" << endl;
    return;
}

int main() {
    test_label(1);  // in label1 
    test_label(2);  // in label2
}

See Also

Reference

Overview of C++ Statements

switch Statement (C++)