1

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!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
yizheshexin
  • 93
  • 1
  • 6
  • 3
    Labelled break statement. – khelwood Feb 26 '20 at 00:25
  • 1
    It looks like a "label" that can be used as a "goto" – MadProgrammer Feb 26 '20 at 00:25
  • 2
    A [labeled branching statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html) (or loop). – Elliott Frisch Feb 26 '20 at 00:25
  • Does this answer your question? [How to use labels in java code?](https://stackoverflow.com/questions/28381212/how-to-use-labels-in-java-code) – mypetlion Feb 26 '20 at 00:32
  • Always try to make your Stack Overflow question's title descriptive enough that someone can tell what your question is about without needing to click through to read the body. Hence, the edit to replace "this" with a description of what the "this" you're asking about is. – Charles Duffy Feb 26 '20 at 00:43
  • @MadProgrammer, it does not transfer the flow of control to the label like "goto" does. – cowlinator Feb 26 '20 at 00:43
  • @cowlinator But you could use `goto` to transfer control - it's just not used in the example – MadProgrammer Feb 26 '20 at 00:53
  • @MadProgrammer There is a `goto` in Java, but [you cannot use it](https://stackoverflow.com/q/2545103/1270789) to transfer control! – Ken Y-N Feb 26 '20 at 05:59
  • @KenY-N Honestly - personally I avoid labels and gotos in favour a more readable solutions ;) – MadProgrammer Feb 26 '20 at 06:07

2 Answers2

1

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

Gopinath
  • 4,066
  • 1
  • 14
  • 16
1

What you have there is a labeled statement where the statement is a block and also a break statement with label. You don't have a labeled break statement - that is legal but weird.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305