What is "search: { ... }" / "break search" syntax officially called in Java?

yizheshexin :

This code is from leetcode 681's solution:

class Solution {
    public String nextClosestTime(String time) {
        int cur = 60 * Integer.parseInt(time.substring(0, 2));
        cur += Integer.parseInt(time.substring(3));
        Set<Integer> allowed = new HashSet();
        for (char c: time.toCharArray()) if (c != ':') {
            allowed.add(c - '0');
        }

        while (true) {
            cur = (cur + 1) % (24 * 60);
            int[] digits = new int[]{cur / 60 / 10, cur / 60 % 10, cur % 60 / 10, cur % 60 % 10};
            search : {
                for (int d: digits) if (!allowed.contains(d)) break search;
                return String.format("%02d:%02d", cur / 60, cur % 60);
            }
        }
    }
}

Normally I will use Python to do leetcode but recently I want to practice with Java. And I notice this part:

            search : {
                for (int d: digits) if (!allowed.contains(d)) break search;
                return String.format("%02d:%02d", cur / 60, cur % 60);
            }

I have never seen such use before. I can understand what it is doing: feels like it customize a 'search' block and can break from it. And I just want to know what is the official name of such usage in Java. Thanks!

Gopinath :

This construct is called the 'Labeled break' in Java.

More information can be found at

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12539&siteId=1