Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No braces means that the loop affects only the next <strong>statement</strong> that follows the loop.</p> <p>So </p> <pre><code>for (i = 2; i &lt;= n; i = i+2) System.out.println (n + " " + i + " " + j ); </code></pre> <p>is equivalent to </p> <pre><code>for (i = 2; i &lt;= n; i = i+2) { System.out.println (n + " " + i + " " + j ); } </code></pre> <p>Usually, indentation is used in those cases to make the code more comprehensible, like this:</p> <pre><code>for (i = 2; i &lt;= n; i = i+2) System.out.println (n + " " + i + " " + j ); </code></pre> <p><strong>EDIT:</strong> Now, this is the actual answer to the question. It all depends on the different iterations the loop does and how do the variables get incremented.</p> <pre><code>int n = 8; int i = 1; int j = 1; j = n + 2; //This means that j takes the value 10. System.out.println (n + " " + i + " " + j ); // 8 1 10 So far, so good. </code></pre> <p>Now, on with the iteration:</p> <pre><code>while (n &gt; 1) { n = n/2; for (i = 2; i &lt;= n; i = i+2) System.out.println (n + " " + i + " " + j ); j++; } </code></pre> <p>For the first iteration, we have <code>n=8 i=1 j=10</code>, so since <code>n &gt; 0</code> is <code>true</code> the loop takes place.</p> <pre><code>n = n / 2; //n = 4 </code></pre> <p>Then, the <code>for</code> (note that it just assigns the value <code>2</code> to <code>i</code>):</p> <pre><code>for (i = 2; i &lt;= 4; i = i+2) //Since n = 4 </code></pre> <p>Since <code>n = 4</code>, the only values that <code>i</code> can take are <code>2</code> and <code>4</code>, then the prints are:</p> <pre><code>4 2 10 4 4 10 </code></pre> <p>After that, <code>j</code> is incremented by one, making it <code>j = 11</code>. The condition for the <code>while</code> is met again because <code>n = 4</code>. <code>n = n/2</code> makes <code>n</code> take the value <code>2</code>, so it enters the <code>while again</code>. Let's take a look at the for again:</p> <pre><code>for (i = 2; i &lt;= 2; i = i+2) //Since n = 2 </code></pre> <p>This time, the only value that <code>i</code> can take is <code>2</code> (note that the value of <code>i</code> is reset again to <code>2</code> while starting the iteration), and that's the print you get.</p> <pre><code>2 2 11 </code></pre> <p>Before iterating again, <code>j++</code> makes <code>j</code> have the value <code>12</code>.</p> <p>On the final iteration, <code>n = n/2</code> results in <code>n = 1</code> since n is an <code>int</code> but this is done inside the while, so it enters again. Now that <code>n = 1</code> the loop looks like this:</p> <pre><code>for (i = 2; i &lt;= 1; i = i+2) //Since n = 1 </code></pre> <p><code>i</code> is set to <code>2</code> and the condition for the for is not met (<code>2 &lt;= 1</code> is <code>false</code>). Then there is no print this time, yet <code>j</code> is incremented to <code>13</code> at the end of the while.</p> <p>In the next iteration you have <code>n = 1</code>, that means that the <code>while</code>'s condition is not met so the iteration breaks. Finally you have <code>n = 1</code>, <code>i = 2</code> and <code>j = 13</code>. That's the last print you get.</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.
    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