Print the numbers 0 to 4:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
The for
loop loops through a block of code a number of times.
From the example above:
There is also a "for-each" loop, which is used exclusively to loop through elements in an array:
The following example outputs all elements in the cars array, using a "for-each" loop:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
Read more about for loops in our Java For Loop Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!