Please explain the usage of Labeled Statements

后端 未结 3 1427
清歌不尽
清歌不尽 2020-11-27 20:50
  • Is breaking and continuing the only uses of labeled statements in Java?
  • When have you used Labeled Statements in your programs?

Sorry the c

3条回答
  •  情书的邮戳
    2020-11-27 21:17

    Yes, break and continue are the only two uses for labeled statements in Java. (Java has no goto statement.)

    You can use a label to break out of nested loops.

    outer:
        for (int i = 0; i < 5; i++) {    
            for (int j = 0; j < 5; j++) {
                System.out.println("Hello");
                continue outer;
            } // end of inner loop
            System.out.println("outer"); // Never prints
        }
    System.out.println("Good-Bye");
    

    When you continue back to the outer label, you're skipping the remainder of both the inner and the outer loop, including the print statement.

提交回复
热议问题