Note that there are some explanatory texts on larger screens.

plurals
  1. POjava: while loop - statement with a semicolon before going into statements between curly braces?
    primarykey
    data
    text
    <p>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. </p> <p>Under exactly which condition is "var" incremented? Either an explanation, or a link that explains similar syntax:</p> <pre><code>while (checkSomeBool) var++; { //other stuff happening in here } </code></pre> <p>would be appreciated. Thank you. Below is the code taken from <a href="https://stackoverflow.com/questions/3623509/how-to-sort-an-array-using-minimum-number-of-writes">CycleSort</a></p> <pre><code>public static final &lt;T extends Comparable&lt;T&gt;&gt; int cycleSort(final T[] array) { int writes = 0; // Loop through the array to find cycles to rotate. for (int cycleStart = 0; cycleStart &lt; array.length - 1; cycleStart++) { T item = array[cycleStart]; // Find where to put the item. int pos = cycleStart; for (int i = cycleStart + 1; i &lt; array.length; i++) if (array[i].compareTo(item) &lt; 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. &lt;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 &lt; array.length; i++) if (array[i].compareTo(item) &lt; 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; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload