Do loops are executed at least once. At the end of the loop, a stopping condition is checked. If that condition evaluates to true, the loop stops. If not, it repeats until the condition is met. Care must be taken to make sure that the stopping condtion can be met otherwise the program may be trapped in an infinite loop.
The following program allows a user to type in numbers until they are sick of doing so. They then type in 999 and get to see the total of all of the numbers that they have entered.
int total = 0;
int numEntered;
Console.Write("Enter a number or 999 to finish: ");
numEntered = System.Convert.ToInt32(Console.ReadLine());
do
{
total += numEntered;
Console.Write("Enter a number or 999 to finish: ");
numEntered = System.Convert.ToInt32(Console.ReadLine());
}
while (numEntered != 999);
Console.WriteLine("The total of the numbers entered is {0}", total);
Console.ReadLine();
One of the main advantages of the do loop is that its stopping condition can be expressed to allow for the number of iterations to vary at runtime depending on the value of variables.