1、将for语句的第一部分留空
int v = 0; for (; v < 2; v++) { System.out.println(v); Thread.sleep(1000); }
2、while (true)循环相同的写法
int v=1;
for (;;) {
System.out.println(v); Thread.sleep(1000); }
3、多个变量的写法
int j = 0;
for (int k = 0; k < 10; k++, j++) {
}
或者
int j = 0;
for (int k = 0; k < 10 || j < 10; k++, j++) {
}
或者
for (int k = 0, j = 0; k < 10 || j < 10; k++, j++) {
}
4、简洁的写法
public class Main {
public static void main(String[] args) {
int[] integers = { 10, 20, 30 };
for (int x : integers) {
System.out.println(x);
}
}
}
5、for语句的一般形式
官方文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
for (initialization; termination; increment) {
statement(s)
}