Enhanced "for" Statements

This section describes enhanced 'for' statement, which is a looping statement that executes the contained statement repeatedly for each element in a given list.

What Is Enhanced "for" Statement? - An enhanced "for" statement is a looping statement that executes the contained statement repeatedly for each element in a given list.

Here is the syntax for an enhanced "for" statement.

for (type e: list) contained_statement

When an enhanced "for" statement is executed, the flow of control can be described as:

Step 1. Declare a local variable "e".

Step 2. Verify if the "list" has next element.

Step 3. Assign the next element from the "list" to "e", if next element exists.

Step 4. End the "for" statement, if there is no next element.

Step 5. Run "contained_statement".

Step 6. Go back to Step 2.

Note that the "list" can be an array object, or an "Iterable" object. Here is a sample program that shows you how to use enhanced "for" statements:

/* EnhancedForStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.util.*;
class EnhancedForStatementTest {
   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;
      int max = 20;
      int sum = 0;

      out.println("Enhanced \"for\" statement on an array object:");
      Integer[] arrayList = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,
                             11,12,13,14,15,16,17,18,19,20};
      for (Integer i :  arrayList) sum += i;
      out.println("   Sum of 1 to 20: "+sum);
      sum = 0;

      out.println("Enhanced \"for\" statement on an iterable object:");
      Iterable<Integer> iterableList = Arrays.<Integer>asList(arrayList);
      for (Integer i :  iterableList) sum += i;
      out.println("   Sum of 1 to 20: "+sum);
      sum = 0;
   }
}

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

herong> java EnhancedForStatementTest.java

Enhanced "for" statement on an array object:
   Sum of 1 to 20: 210

Enhanced "for" statement on an iterable object:
   Sum of 1 to 20: 210

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

 Outdated Tutorials

 References

 Full Version in PDF/EPUB