这不是错误。分号成为
for循环体中唯一的“语句” 。
编写另一种方法可以使其更容易看到:
for (int i = 0; i < 100; i++) ;{ count++;}由于分号,
count++带有的块变成只有一条语句的裸块,该语句根本不与
for循环关联。因此,此块及其
count++内部仅执行 一次 。
这在语法上是有效的java。
for (int i = 0; i < 100; i++);等效于:
for (int i = 0; i < 100; i++){ ; } // no statement in the body of the loop.for由于循环增量语句或终止条件中的副作用,这种形式的循环可能很有用。例如,如果您想编写自己的代码
indexOfSpace以在中找到空格字符的第一个索引
String:
int idx;// for loop with no body, just incrementing idx:for (idx = 0; string.charAt(idx) != ' '; idx++);// now idx will point to the index of the ' '



