Easy Tutorial
❮ Java Print Rect Dir Delete ❯

Java Example - Label

Java Examples

Labels in Java are designed for loops, intended to facilitate the use of break and continue in nested loops.

The following example demonstrates jumping to a specified label when break or continue is used in a loop:

Main.java File

public class Main {
    public static void main(String[] args) {
        String strSearch = "This is the string in which you have to search for a substring.";
        String substring = "substring";
        boolean found = false;
        int max = strSearch.length() - substring.length();
        testlbl:
        for (int i = 0; i <= max; i++) {
            int length = substring.length();
            int j = i;
            int k = 0;
            while (length-- != 0) {
                if(strSearch.charAt(j++) != substring.charAt(k++)){
                    continue testlbl;
                }
            }
            found = true;
            break testlbl;
        }
        if (found) {
            System.out.println("Substring found.");
        }
        else {
            System.out.println("Substring not found in the string.");
        }
    }
}

The above code outputs the following result:

Substring found.

Java Examples

❮ Java Print Rect Dir Delete ❯