Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
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
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
What Is Control Flow Statement
Nested "if-then-else" Statements
Fall-Through Behavior of "switch" Statements
►Labeled "continue" Statements
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
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