Labeled "continue" Statements

This section describes labeled 'continue' statement, which is a branching statement that transfers the control to the end of the labeled enclosing loop block and continues the next iteration of the labeled loop.

What Is Labeled "continue" Statement? - A labeled "continue" statement is a branching statement that transfers the control to the end of the labeled enclosing loop block and continues the next iteration of the labeled loop.

Here is the syntax for a labeled "continue" statement.

loop_label : while|do|for ... { // "continue" continues here
   ...
   while|do|for ... {
      ...
      continue loop_label
      ...
   }
   ...
}

Labeled "continue" statements extend usages of non-labeled "continue" statements in 1 situation:

Here is a sample program that shows you how to use labeled "continue" statements:

/* LabeledContinueStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.nio.file.*;
import java.time.*;
class LabeledContinueStatementTest {
   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;

      out.println("Labeled \"continue\" statement for outer loop:");
      loopOuter: for (int i = 3; i < 20; i++) {
         boolean isPrime = true;
         int j = 2;
         while (j < i) {
            isPrime = i%j > 0;
            if (!isPrime) continue loopOuter;
            j++;
         }
         out.println("   "+i+" is a prime number.");
      }
   }
}

If you compile and run the above program, you will see:

herong> java LabeledContinueStatementTest.java

Labeled "continue" statement for outer loop:
   3 is a prime number.
   5 is a prime number.
   7 is a prime number.
   11 is a prime number.
   13 is a prime number.
   17 is a prime number.
   19 is a prime number.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

 "if-then" Statements

 "if-then-else" Statements

 Nested "if-then-else" Statements

 "switch" Statements

 Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

 "while" Statements

 "do" Statements

 "break" Statements

 Labeled "break" Statements

 "continue" Statements

Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB