Skip to content

Labeled Statement

LabeledStmt Entity

In Java, a labeled statement allows you to assign an identifier (label) to a specific block of code or statement. Labels are most commonly used with break and continue statements to control the flow of nested loops or blocks. When using a labeled statement, you place the label name followed by a colon (:) before the statement or block you want to label. This is particularly useful when you need to break out of or continue a specific outer loop from within a nested loop structure.

Example

outer: // Label for the outer loop
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break outer; // Breaks out of the outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}
// Example with continue and label
search:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] == -1) {
continue search; // Skips to the next iteration of outer loop
}
// Process array[i][j]
}
}

Attributes

MethodDescription
GetAPrimaryQlClass() stringReturns the primary CodeQL class name for this entity
GetHalsteadID() intReturns the Halstead complexity metric identifier for the labeled statement
GetLabel() *LabeledStmtReturns a pointer to the labeled statement itself
GetPP() stringReturns the pretty-printed representation of the labeled statement
ToString() stringReturns a string representation of the labeled statement