[英]java: while loop - statement with a semicolon before going into statements between curly braces?
我在这里查看关于stackoverflow的另一页,并遇到了循环排序的有效实现,但是我不明白在while循环中的花括号之前如何使用带分号的语句。 我认为while循环应该完全终止,并且一旦找到带有分号的语句就不再采取进一步的操作,那么花括号内的代码如何执行? 乍一看,我将其解释为“ var”随着while循环的每次迭代而增加-但我知道并非如此,因为将其从该位置删除并将“ var ++”放在花括号内会导致无穷大环。
究竟在哪种条件下“ var”会增加? 一个解释,或一个解释相似语法的链接:
while (checkSomeBool) var++;
{
//other stuff happening in here
}
将不胜感激。 谢谢。 下面是从CycleSort中获取的代码
public static final <T extends Comparable<T>> int cycleSort(final T[] array) {
int writes = 0;
// Loop through the array to find cycles to rotate.
for (int cycleStart = 0; cycleStart < array.length - 1; cycleStart++) {
T item = array[cycleStart];
// Find where to put the item.
int pos = cycleStart;
for (int i = cycleStart + 1; i < array.length; i++)
if (array[i].compareTo(item) < 0) pos++;
// If the item is already there, this is not a cycle.
if (pos == cycleStart) continue;
// Otherwise, put the item there or right after any duplicates.
<while (item.equals(array[pos])) pos++;
{
final T temp = array[pos];
array[pos] = item;
item = temp;
}
writes++;
// Rotate the rest of the cycle.
while (pos != cycleStart) {
// Find where to put the item.
pos = cycleStart;
for (int i = cycleStart + 1; i < array.length; i++)
if (array[i].compareTo(item) < 0) pos++;
// Put the item there or right after any duplicates.
while (item.equals(array[pos])) pos++;
{
final T temp = array[pos];
array[pos] = item;
item = temp;
}
writes++;
}
}
return writes;
}
while
循环以var++
结尾
while (checkSomeBool) var++; // while ends here
之后的代码根本不是while
循环的一部分。
{
//other stuff happening in here - not part of the while loop
}
类似于C的语言,您可以使用括号将任意代码括起来以创建块作用域,无论是否带有其他语法构造。
大括号中的代码将在循环后作为普通代码执行。
如果完全取出while
行,它将继续运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.