user2856358
user2856358

Reputation: 37

Difference between Do/While and While/Do

I am trying to learn C# and I want to take this code I wrote and see if I can change it and use do/while and while/do in it. I have been messing with this all day and I have a hard time grasping the do/while and while/do for some reason. If anyone can show me how it would be great. Also, if you do or can, please explain it a bit too. Trying to learn, but I'm puzzled.

private void calculate_Click(object sender, EventArgs e)
{
    int answer = 0;
    //int i = int.Parse(textBox1.Text);
    int j = int.Parse(textBox2.Text);
    int multiplyBy = int.Parse(multiplier.Text);

    for (int i = int.Parse(textBox3.Text); i <= j; i++)
    {
        //answer = answer + i;
        //listBox1.Items.Add(answer.ToString());
        answer = multiplyBy * i;
        listBox1.Items.Add(i + " times" + multiplyBy + " = " + answer.ToString());
    }
}

Upvotes: 1

Views: 345

Answers (3)

Kage Ong
Kage Ong

Reputation: 1

Use do/while when your body of loop MUST execute at least one time, else, use while.

Upvotes: 0

Nicholas Carey
Nicholas Carey

Reputation: 74177

  • while ( condition ) { LoopBody() }
    executes LoopBody() zero or more times.

    condition is checked prior to executing the loop body: so long as condition is true, the loop body is executed.

  • do { LoopBody() } while ( condition )
    executes LoopBody() one or more times.

    condition is checked after executing the loop body. It is the exact equivalent of

    LoopBody() ;
    while ( condition )
    {
      LoopBody() ;
    }
    

One should note that if the body of the loop never changes the condition, the loop while never terminate.

Edited to note: for is for all intents and purposes a while loop. Your for loop:

for (int i = int.Parse(textBox3.Text); i <= j; i++)
{
  LoopBody() ;
}

is roughly equivalent to:

int i = int.Parse(textBox3.Text) ;
while ( i <= j )
{
  LoopBody() ;
  i++ ;
}

The "roughly bit is because the loop indexer is scoped to the for loop as if it were written

{ // introduces a scope int i = int.Parse(textBox3.Text) ; while ( i <= j ) { LoopBody() ; i++ ; } } // end of scope

And that's not quite the same as the for loop, because were you to introduce a new i in the method, you'd get a compiler whine about duplicate variable names. A for-loop doesn't have that problem. The compiler is perfectly happy with something like this:

if ( condition-A )
{
  for ( int i = 0 ; i < 10 ; ++i )
  {
     DoSomethingUseful(i) ;
  }
}
else
{
  for ( int i = 0 ; i < 10 ; ++i )
  {
     DoSomethingElseUseful(i) ;
  }
}

whereas something like this would cause the compiler to whine:

if ( condition-A )
{
  int i = 0 ;
  while ( i < 10 )
  {
     DoSomethingUseful(i) ;
     ++i ;
  }
}
else
{
  int i = 0 ;
  while ( i < 10 )
  {
     DoSomethingElseUseful(i) ;
     ++i ;
  }
}

Hope this helps.

Refactoring your code to use a while loop would give you something like this:

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 <= j )
  {
    int    answer = multiplyBy * i ;
    string value  = string.Format( "{0} times {1} = {2}" , i , mulitplyBy , answer ) ;

    listBox1.Items.Add( value ) ;

    ++i ;
  }

  return ;
}

Upvotes: 8

Brian
Brian

Reputation: 5119

The general form of do/while looks like this:

do
{
   // Perform some actions here.
} while (some condition);  // Break out of the loop once this condition is satisfied.  

The 'some condition' is of type bool. The main difference between a do/while and a while is that if 'some condition' is false at the beginning of the do loop, the body of the loop will not be executed.

The basic point of the do/while loop is to spin through the loop performing some action or actions and then break out of the loop once your while condition is satisfied. For reference, you can see a very simple example here from MSDN. For the most part, as you gain more experience, you will likely find yourself using for, foreach and while loops over the do/while variety.

Upvotes: 0

Related Questions