Documentation

The Java™ Tutorials
Hide TOC
The for Statementfor语句
Trail: Learning the Java Language
Lesson: Language Basics
Section: Control Flow Statements

The for Statementfor语句

The for statement provides a compact way to iterate over a range of values.for语句提供了一种对一系列值进行迭代的紧凑方法。Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied.程序员经常将其称为“for循环”,因为它重复循环直到满足特定条件的方式。The general form of the for statement can be expressed as follows:for语句的一般形式可以表示为:

for (initialization; termination;
     increment) {
    statement(s)
}

When using this version of the for statement, keep in mind that:使用此版本的for语句时,请记住:

The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:以下程序ForDemo使用for语句的一般形式将数字1到10打印到标准输出:

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: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count 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 the for statement, so it can be used in the termination and increment expressions as well.此变量的范围从其声明扩展到由for语句控制的块的末尾,因此它也可以用于终止表达式和增量表达式。If the variable that controls a for statement is not needed outside of the loop, it's best to declare the variable in the initialization expression.如果控制for语句的变量不需要在循环之外,最好在初始化表达式中声明该变量。The names i, j, and k are often used to control for loops; declaring them within the initialization expression limits their life span and reduces errors.名称ijk通常用于控制循环;在初始化表达式中声明它们会限制它们的寿命并减少错误。

The three expressions of the for loop are optional; an infinite loop can be created as follows:for循环的三个表达式是可选的;可以按如下方式创建无限循环:

// infinite loop
for ( ; ; ) {
    
    // your code goes here
}

The for statement also has another form designed for iteration through Collections and arrays.for语句还有另一个形式,用于通过集合数组进行迭代。This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.该表单有时被称为增强的for语句,可用于使循环更紧凑、更易于阅读。To demonstrate, consider the following array, which holds the numbers 1 through 10:为了演示,考虑下面的数组,它保持数字1到10:

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

The following program, EnhancedForDemo, uses the enhanced for to loop through the array:以下程序EnhancedForDemo使用增强的for在数组中循环:

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 variable item holds the current value from the numbers array.在本例中,变量item保存数字数组中的当前值。The output from this program is the same as before:该程序的输出与之前相同:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

We recommend using this form of the for statement instead of the general form whenever possible.我们建议尽可能使用这种形式的for语句,而不是一般形式。


Previous page: The while and do-while Statements
Next page: Branching Statements