簡體   English   中英

Java:while循環-進入花括號之間的語句之前用分號聲明?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM