Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li><p><code>while ( condition ) { LoopBody() }</code><br/>executes <code>LoopBody()</code> zero or more times.</p> <p><code>condition</code> is checked <strong>prior</strong> to executing the loop body: so long as <code>condition</code> is <code>true</code>, the loop body is executed. </p></li> <li><p><code>do { LoopBody() } while ( condition )</code><br/>executes <code>LoopBody()</code> one or more times.</p> <p><code>condition</code> is checked <strong>after</strong> executing the loop body. It is the <strong>exact</strong> equivalent of</p> <pre><code>LoopBody() ; while ( condition ) { LoopBody() ; } </code></pre></li> </ul> <p>One should note that if the body of the loop never changes the condition, the loop while never terminate.</p> <p>Edited to note: <code>for</code> is for all intents and purposes a while loop. Your <code>for</code> loop:</p> <pre><code>for (int i = int.Parse(textBox3.Text); i &lt;= j; i++) { LoopBody() ; } </code></pre> <p>is roughly equivalent to:</p> <pre><code>int i = int.Parse(textBox3.Text) ; while ( i &lt;= j ) { LoopBody() ; i++ ; } </code></pre> <p>The "roughly bit is because the loop indexer is scoped to the <code>for</code> loop as if it were written</p> <p>{ // introduces a scope int i = int.Parse(textBox3.Text) ; while ( i &lt;= j ) { LoopBody() ; i++ ; } } // end of scope</p> <p>And that's not quite the same as the for loop, because were you to introduce a new <code>i</code> in the method, you'd get a compiler whine about duplicate variable names. A <code>for</code>-loop doesn't have that problem. The compiler is perfectly happy with something like this:</p> <pre><code>if ( condition-A ) { for ( int i = 0 ; i &lt; 10 ; ++i ) { DoSomethingUseful(i) ; } } else { for ( int i = 0 ; i &lt; 10 ; ++i ) { DoSomethingElseUseful(i) ; } } </code></pre> <p>whereas something like this would cause the compiler to whine:</p> <pre><code>if ( condition-A ) { int i = 0 ; while ( i &lt; 10 ) { DoSomethingUseful(i) ; ++i ; } } else { int i = 0 ; while ( i &lt; 10 ) { DoSomethingElseUseful(i) ; ++i ; } } </code></pre> <p>Hope this helps.</p> <p>Refactoring your code to use a <code>while</code> loop would give you something like this:</p> <pre><code>private void calculate_Click(object sender, EventArgs e) { int i = int.Parse( textBox3.Text ) ; int j = int.Parse( textBox2.Text ) ; int multiplyBy = int.Parse( multiplier.Text ) ; while ( i &lt;= j ) { int answer = multiplyBy * i ; string value = string.Format( "{0} times {1} = {2}" , i , mulitplyBy , answer ) ; listBox1.Items.Add( value ) ; ++i ; } return ; } </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