Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, there is no such situation. Every <code>do</code>-<code>while</code> loop can be written in terms of a <code>while</code>-loop (by executing the body once before the loop) and vice versa. In turn, every <code>while</code>-loop</p> <pre><code>while (X) { ... } </code></pre> <p>can be written as</p> <pre><code>for (; X;) { ... } </code></pre> <p>i.e. we omit an initialization and an increment statement. We can also convert from a <code>for</code> back to a <code>while</code> by correctly placing the initialization and increment.</p> <p>In short, it's always possible to convert from one loop variant to either of the other two. <code>for</code>-loops just give you the benefit of being able to limit the scope of a loop control variable and do any incrementation at the top. It goes without saying that in many cases one particular loop variant makes much more sense to use than the others; each has its specific use cases.</p> <p>Also note that the fun doesn't just end with loops: it's also possible to convert every loop into a recursive function and vice versa (although in practice there could be limitations to this; for example a loop that worked fine could, when converted to a recursive function, produce a stack overflow error).</p> <hr> <blockquote> <p>[I]s <code>while</code>/<code>do</code>-<code>while</code> in any way "distinct" from <code>for</code>?</p> </blockquote> <p>It is not. For instance, the bytecode of the following two snippets is identical:</p> <pre><code>int x = 0; while (x &lt; 10) { x++; } </code></pre> <p>and</p> <pre><code>int x = 0; for (; x &lt; 10;) { // or: for (; x &lt; 10; x++) {} x++; } </code></pre> <p>both become:</p> <pre><code> 0: iconst_0 1: istore_1 2: goto 8 5: iinc 1, 1 8: iload_1 9: bipush 10 11: if_icmplt 5 14: return </code></pre> <hr> <p>There was talk in the comments about <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html"><code>for</code>-each</a> loops and that they might be intrinsically different from the other loop types. This is absolutely not true; <code>for</code>-each loops are pure syntactic sugar around iterators (or looping over arrays). Every <code>for</code>-each loop can also be converted to each of the other loop types. Here's an example:</p> <pre><code>for (String s : l) { // l is a list of strings System.out.println(s); } </code></pre> <p>and</p> <pre><code>String s; Iterator&lt;String&gt; iter = l.iterator(); // l is a list of strings while (iter.hasNext()) { s = iter.next(); System.out.println(s); } </code></pre> <p>both become:</p> <pre><code> 24: invokeinterface #33, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 29: astore_3 30: goto 50 33: aload_3 34: invokeinterface #39, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 39: checkcast #19 // class java/lang/String 42: astore_2 43: getstatic #45 // Field java/lang/System.out:Ljava/io/PrintStream; 46: aload_2 47: invokevirtual #51 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 50: aload_3 51: invokeinterface #57, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 56: ifne 33 </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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