Zebra0.com

csharp loopsdo loops test condition at end, and execute 1 or more times

do loops test condition at end, and execute 1 or more times

A do loop tests the condition at the end of the loop.

The do while loop is similar to the while loop except that it does the test at the end of the loop.
A do loop executes one or more times. The format of a do while loop is:

do
{
statements;
} while(Boolean expression);

 blast off

Example: 

private void Form1_Load(object sender, EventArgs e)
{
  // Add 10,9,8,7,6,5,4,3,2,1,Blast OFF! to a list box.
  int i = 10;
  do
  {
    listBox1.Items.Add(i);
    i--;
  } while (i > 0);
  listBox1.Items.Add("Blast off");
}

Notice that the loop continues as long as the Boolean expression is true. When
it becomes false, the next statment after the loop executes.
A do loop always executes at least once.

NEXT: Endless do loops