The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Thefor
statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of thefor
statement can be expressed as follows:
for (initialization;termination;increment) {statement(s)}
When using this version of thefor
statement, keep in mind that:
false
, the loop terminates.The following program,ForDemo
, uses the general form of thefor
statement to print the numbers 1 through 10 to standard output:
class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } }}
The output of this program is:
Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5Count is: 6Count is: 7Count is: 8Count is: 9Count is: 10
Notice how the code declares a variable within the initialization expression. The scope of this variable extends from its declaration to the end of the block governed by thefor
statement, so it can be used in the termination and increment expressions as well. If the variable that controls afor
statement is not needed outside of the loop, it's best to declare the variable in the initialization expression. The namesi
,j
, andk
are often used to controlfor
loops; declaring them within the initialization expression limits their life span and reduces errors.
The three expressions of thefor
loop are optional; an infinite loop can be created as follows:
// infinite loopfor ( ; ; ) { // your code goes here}
Thefor
statement also has another form designed for iteration throughCollections andarrays This form is sometimes referred to as theenhanced for statement, and can be used to make your loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
The following program,EnhancedForDemo
, uses the enhancedfor
to loop through the array:
class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } }}
In this example, the variableitem
holds the current value from the numbers array. The output from this program is the same as before:
Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5Count is: 6Count is: 7Count is: 8Count is: 9Count is: 10
We recommend using this form of thefor
statement instead of the general form whenever possible.
About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights
Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.