"continue" Statements

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

"continue" statements have 2 forms: non-labeled "continue" statements and labeled "continue" statements. Let's look at non-labeled "continue" statements in this tutorial first.

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

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

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

Note that non-labeled "continue" statements can not be used in statements other than "while", "do", or "for" statements.

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

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

      out.println("\"continue\" statement in a single-level loop:");
      long bytes = 0;
      int counts = 0;
      Path dir = FileSystems.getDefault().getPath(arg[0]);
      try {
         DirectoryStream<Path> stream = Files.newDirectoryStream(dir);

         for (Path path : stream) { // "continue" continues here
            File file = path.toFile();

            if (file.isDirectory()) continue;
            bytes += file.length();
            counts++;
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      out.println("   # of files: "+counts);
      out.println("   # of bytes: "+bytes);

      out.println("\"continue\" statement in a two-level loop:");
      LocalDate now = LocalDate.now();
      int year = now.getYear();

      for (int y = year-5; y < year; y++) {

         counts = 0;
         LocalDate d = LocalDate.of(y, 1, 1);

         for ( ; d.getYear() == y; d = d.plusDays(1) ) { // continues here

            if (d.getDayOfWeek() != DayOfWeek.SUNDAY)  continue;
            counts++;
         }
         out.println("   "+counts+" Sundays in year "+y);
      }
   }
}

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

herong> java ContinueStatementTest.java .

"continue" statement in a single-level loop:
   # of files: 13
   # of bytes: 11827

"continue" statement in a two-level loop:
   52 Sundays in year 2015
   52 Sundays in year 2016
   53 Sundays in year 2017
   52 Sundays in year 2018
   52 Sundays in year 2019

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