Java For 循环终止参数
Java For Loop Termination Parameter
所以我知道您可以在 for 循环的每个参数中包含多个语句,例如:
for(int i=0, int j=0; i<10 , j<14; i++, j=j+2){}
但是第二个参数是被视为"and"语句还是"or"语句?它会在 "j" 大于 14 时停止,还是会继续直到 "i" 也大于 10?
那不会编译。第二个参数不能有逗号分隔列表。这将编译:
for(int i=0, j=0; i<10 && j<14; i++, j=j+2){}
终止参数必须是单个逻辑运算符,i<10 && j<14
可以,i<10 , j<14
不行。
这会把事情弄清楚。 运行 程序
i 和 j 是 2 个变量,它们的使用取决于你想如何使用它。
如果你不改变j的值,那么它可以作为终止符。
public class TestProgram {
public static void main(String[] args){
for (int i = 0, j = 1, k = 2; i < 5; i++) {
System.out.println("I : " + i + ",j : " + j + ", k : " + k);
}
/*
* Please note that the variables which are declared, should be of same
* type as in this example int.
*/
// THIS WILL NOT COMPILE
// for(int i=0, float j; i < 5; i++);
}
来自JLS 14.14.1 The basic for
Statement:
BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
第一部分,[ForInit]必须是一个变量或一组相同类型的变量的声明:
If the ForInit code is a list of statement expressions (§14.8), the expressions are evaluated in sequence from left to right; their values, if any, are discarded.
第二部分,[表达式]必须是boolean
:
The Expression must have type boolean
or Boolean
, or a compile-time error occurs
因此,您当前的 for
循环声明不符合这些条件。让我们看看为什么:
for(
int i=0, int j=0; //you cannot declare a list of variables like this
//you can test this by moving this piece of code
//out of the for loop
i<10 , j<14; //this expression doesn't return a boolean nor a Boolean
i++, j=j+2 //this one is right
) {
//...
}
因此,声明此 for
循环的正确方法是:
for(
int i=0, j=0;
i<10 && j<14;
i++, j=j+2
) {
//...
}
其中,在单个 LoC 中将是:
for(int i=0, j=0; i<10 && j<14; i++, j=j+2) {
//...
}
所以我知道您可以在 for 循环的每个参数中包含多个语句,例如:
for(int i=0, int j=0; i<10 , j<14; i++, j=j+2){}
但是第二个参数是被视为"and"语句还是"or"语句?它会在 "j" 大于 14 时停止,还是会继续直到 "i" 也大于 10?
那不会编译。第二个参数不能有逗号分隔列表。这将编译:
for(int i=0, j=0; i<10 && j<14; i++, j=j+2){}
终止参数必须是单个逻辑运算符,i<10 && j<14
可以,i<10 , j<14
不行。
这会把事情弄清楚。 运行 程序
i 和 j 是 2 个变量,它们的使用取决于你想如何使用它。
如果你不改变j的值,那么它可以作为终止符。
public class TestProgram {
public static void main(String[] args){
for (int i = 0, j = 1, k = 2; i < 5; i++) {
System.out.println("I : " + i + ",j : " + j + ", k : " + k);
}
/*
* Please note that the variables which are declared, should be of same
* type as in this example int.
*/
// THIS WILL NOT COMPILE
// for(int i=0, float j; i < 5; i++);
}
来自JLS 14.14.1 The basic for
Statement:
BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
第一部分,[ForInit]必须是一个变量或一组相同类型的变量的声明:
If the ForInit code is a list of statement expressions (§14.8), the expressions are evaluated in sequence from left to right; their values, if any, are discarded.
第二部分,[表达式]必须是boolean
:
The Expression must have type
boolean
orBoolean
, or a compile-time error occurs
因此,您当前的 for
循环声明不符合这些条件。让我们看看为什么:
for(
int i=0, int j=0; //you cannot declare a list of variables like this
//you can test this by moving this piece of code
//out of the for loop
i<10 , j<14; //this expression doesn't return a boolean nor a Boolean
i++, j=j+2 //this one is right
) {
//...
}
因此,声明此 for
循环的正确方法是:
for(
int i=0, j=0;
i<10 && j<14;
i++, j=j+2
) {
//...
}
其中,在单个 LoC 中将是:
for(int i=0, j=0; i<10 && j<14; i++, j=j+2) {
//...
}