java syntax colon writing loop naming specify loop break

In the code, bufferLoop and charLoop are named for the loops. In this way, when there are nested loops, break can specify which loop to jump out of.

Break and continue in Java can jump out of the specified loop.

If no loop name is added after break and continue, the loop in which they are located will be jumped out by default. By adding the loop name after it, you can jump out of the specified loop (usually name the outer loop).

However, sonar gave suggestions not to use it in this way, saying that it does not comply with the universal standards, and gave normative suggestions. However, in some cases, the judgment conditions are slightly complicated to implement the standardized writing method.

How to write loop naming in BufferedReader:

package java.io;

public class BufferedReader extends Reader {

    String readLine(boolean ignoreLF) throws IOException {
        StringBuffer s = null;
        int startChar;

        synchronized (lock) {
            ensureOpen();
            boolean omitLF = ignoreLF || skipLF;

        bufferLoop:
            for (;;) {

                if (nextChar >= nChars)
                    fill();
                if (nextChar >= nChars) { /* EOF */
                    if (s != null & amp; & amp; s.length() > 0)
                        return s.toString();
                    else
                        return null;
                }
                boolean eol = false;
                char c = 0;
                int i;

                /* Skip a leftover '\\
', if necessary */
                if (omitLF & amp; & amp; (cb[nextChar] == '\\
'))
                    nextChar + + ;
                skipLF = false;
                omitLF = false;

            charLoop:
                for (i = nextChar; i < nChars; i + + ) {
                    c = cb[i];
                    if ((c == '\\
') || (c == '\r')) {
                        eol = true;
                        break charLoop;
                    }
                }

                startChar = nextChar;
                nextChar = i;

                if (eol) {
                    String str;
                    if (s == null) {
                        str = new String(cb, startChar, i - startChar);
                    } else {
                        s.append(cb, startChar, i - startChar);
                        str = s.toString();
                    }
                    nextChar + + ;
                    if (c == '\r') {
                        skipLF = true;
                    }
                    return str;
                }

                if(s==null)
                    s = new StringBuffer(defaultExpectedLineLength);
                s.append(cb, startChar, i - startChar);
            }
        }
    }
}

Test code:

 int matrix[][] = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };


        outer:
        for (int row = 0; row < matrix.length; row + + ) {

            inner:
            for (int col = 0; col < matrix[row].length; col + + ) {

                if (col == row) {
                    continue inner;
                    //continue outer;
                }
                System.out.println(matrix[row][col]);
            }
        }

====================Separating line======================

The article has ended here, the following is purple sweet potato pudding

packagejava.io;

public class BufferedReader extends Reader {

String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;

synchronized (lock) {
ensureOpen();
boolean omitLF = ignoreLF || skipLF;

bufferLoop:
for (;;) {

if (nextChar >= nChars)
fill();
if (nextChar >= nChars) { /* EOF */
if (s != null & amp; & amp; s.length() > 0)
return s.toString();
else
return null;
}
boolean eol = false;
char c = 0;
int i;

/* Skip a leftover ‘\\
‘, if necessary */
if (omitLF & amp; & amp; (cb[nextChar] == ‘\\
‘))
nextChar + + ;
skipLF = false;
omitLF = false;

charLoop:
for (i = nextChar; i < nChars; i + + ) {
c = cb[i];
if ((c == ‘\\
‘) || (c == ‘\r’)) {
eol = true;
break charLoop;
}
}

startChar = nextChar;
nextChar = i;

if (eol) {
String str;
if (s == null) {
str = new String(cb, startChar, i – startChar);
} else {
s.append(cb, startChar, i – startChar);
str = s.toString();
}
nextChar + + ;
if (c == ‘\r’) {
skipLF = true;
}
return str;
}

if(s==null)
s = new StringBuffer(defaultExpectedLineLength);
s.append(cb, startChar, i – startChar);
}
}
}
}

int matrix[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

outer:
for (int row = 0; row < matrix.length; row + + ) {

inner:
for (int col = 0; col < matrix[row].length; col + + ) {

if (col == row) {
continue inner;
//continue outer;
}
System.out.println(matrix[row][col]);
}
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137297 people are learning the system