Java:while循环-进入大括号之间的语句之前,请使用分号声明? [英] java: while loop - statement with a semicolon before going into statements between curly braces?

查看:256
本文介绍了Java:while循环-进入大括号之间的语句之前,请使用分号声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看关于stackoverflow的另一页,并遇到了循环排序的有效实现,但是我不明白在while循环中的花括号之前如何存在带有分号的语句.我认为while循环应该完全终止,并且一旦找到带有分号的语句就不再采取进一步的操作,那么花括号内的代码如何执行?乍一看,我将其解释为"var"随着while循环的每次迭代而增加-但我知道并非如此,因为从该位置删除并将"var ++"放在花括号内会导致无限环形.

I was looking at another page here on stackoverflow and came across a working implementation of cycle sort, but I don't understand how a statement with a semicolon can exist before the curly braces in a while loop. I thought that the while loop is supposed to completely terminate and take no further actions once it finds a statement with a semicolon, so how is it that the code within the curly braces is getting executed? At first glance, I would interpret this as "var" gets incremented with each iteration of the while loop - but I know this is not the case because removing it from that spot and putting the "var++" inside of the curly braces causes an infinite loop.

"var"到底在哪种条件下递增?一个解释,或一个解释相似语法的链接:

Under exactly which condition is "var" incremented? Either an explanation, or a link that explains similar syntax:

while (checkSomeBool) var++;
{
   //other stuff happening in here
}

将不胜感激.谢谢你.以下是从 CycleSort

would be appreciated. Thank you. Below is the code taken from 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循环的一部分.

The code after that is not part of the while loop at all.

{
   //other stuff happening in here - not part of the while loop
}

这篇关于Java:while循环-进入大括号之间的语句之前,请使用分号声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆