Java 这个循环开头的分号是什么意思?
我正在阅读一段开源代码,突然发现了这个分号。我起初认为这是一个错误,但事实并非如此 下面for循环的开括号后面的分号的作用是什么Java 这个循环开头的分号是什么意思?,java,android,Java,Android,我正在阅读一段开源代码,突然发现了这个分号。我起初认为这是一个错误,但事实并非如此 下面for循环的开括号后面的分号的作用是什么 if (nCount > 0){ for(; nCount > 0; nCount--){ if (mBitmaplist[nCount - 1] != null){ mBitmaplist[nCount - 1].recycle();
if (nCount > 0){
for(; nCount > 0; nCount--){
if (mBitmaplist[nCount - 1] != null){
mBitmaplist[nCount - 1].recycle();
mBitmaplist[nCount - 1] = null;
}
}
}
这意味着for循环的初始值设定项部分没有语句
BasicForStatement:
for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement
类似地,如果要跳过for循环的增量部分,它将如下所示
for( ; nCount > 0; ){
// some code
}
// which is like while loop
这是for循环的格式
BasicForStatement:
for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement
您可以看到所有3个都是可选的for(PART1;PART2;PART3){BODY}语句的工作原理如下:
PART1;
<<TOP OF LOOP>>
if PART2 is false then go to <<END OF LOOP>>;
do the BODY;
PART3;
go to <<TOP OF LOOP>>;
<<END OF LOOP>>
PART1;
如果第2部分为false,则转到;
做身体;
第三部分;
去;
如果你说(;PART2;PART3),那只意味着
PART1
什么都不做。(对于第3部分
也是一样。如果你省略了第2部分
,那么什么都不会被测试,转到
就永远不会发生。因此,到达循环末尾的唯一方法是中断
或返回
或其他方法。)希望这个例子能帮助你更好地理解:
public static void main(String[] args) {
int i = 0; // you normally put this before the first semicolon in next line
for (;;) {
if (i > 5) {
break; // this "if" normally goes between the 2 semicolons
}
System.out.println("printing:" + i);
i++; // this is what you put after the second semi-colon
}
}
享受Java的乐趣,继续编写代码 值得一提的是,
For(;;)
是完全有效的Java。它实际上与while(true)
相同。。。还有每一本相当不错的Java教科书。语法来自C。