
Every looping construct has advantages. The C# language provides the do-while loop. This loop is less commonly used and probably has more disadvantages than advantages. It has subtle semantic differences but is most similar to the while loop.
This C# page demonstrates how you can use the do-while loop. The syntax for this loop is demonstrated.

First, here is an example of using the do-while loop to sum the values of the elements in an int array. The int array here is guaranteed to have four elements, so you can avoid checking the length before starting checking its elements in do-while.
Program that uses do-while loop [C#]
class Program
{
static void Main()
{
int[] ids = new int[] { 6, 7, 8, 10 };
//
// Use do-while loop to sum numbers in 4-element array.
//
int sum = 0;
int i = 0;
do
{
sum += ids[i];
i++;
} while (i < 4);
System.Console.WriteLine(sum);
}
}
Output
31
Description. The example code above shows the Main method, which declares an integer array of four values. Next a do-while loop is executed, summing each value in the array.

What about the first index? The length of the array is not checked before adding the first element's value to the sum. Therefore, this code would throw an exception if the array was empty.
Do-while loop syntax. For the do-while loop, you just use the "do" keyword before the loop block curly brackets { }. After the brackets, you specify the condition while. The while is executed after the code is run. The condition's contents are the same as in a regular while loop.
Key point: The important difference with do-while is that no condition is tested before the statements in the loop are executed the first time.
The most common type of loop—other than foreach—is the for loop. It allows you to specify the three conditions right at the start, with semicolons between them. These examples show how you can modify a for loop and turn it into a do-while loop.
For loop [C#]
static int SumFor()
{
//
// Sum numbers 0 .. 4
//
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += i;
}
return sum;
}
Do while loop [C#]
static int SumDoWhile()
{
//
// Sum numbers 0 .. 4
//
int sum = 0;
int i = 0;
do
{
sum += i;
i++;
} while (i < 5);
return sum;
}
While loop [C#]
static int SumWhile()
{
//
// Sum numbers 0 .. 4
//
int sum = 0;
int i = 0;
while (i < 5)
{
sum += i;
i++;
}
return sum;
}
For loop. This loop walks through the indexes 0, 1, 2, 3, and 4. The sum of these numbers is 10, which is the result of the SumFor method. The loop is very compact: it requires three lines of C# code.
For LoopsDo-while loop. This loop also walks through the indexes 0, 1, 2, 3, and 4. However, it will test the index 0 without checking any bounds. The result of this SumDoWhile method is 10. The loop requires five lines of C# code.
While loop. This loop is the same as the do-while loop, but has the condition at the start. It requires five lines of C# code.
While Loop Examples
The critical difference between the for loop and the do-while loop is that in do-while, no conditions are checked before entering the loop. The while loop is the same as the do-while loop, but with the condition at the start.
Note: This difference in the first iteration is important. It is also the reason why do-while loops are probably less useful than other loop constructs. All other loops have a test before the first iteration. The do-while loop is much less common. This makes errors due to human factors more likely.
We inspected the MSIL generated with for and do-while loops. With for, there is an additional "br.s" statement, which "unconditionally transfers control" to somewhere else. However, testing revealed no difference in performance between the loops. It is likely that the JIT optimized the loops to the same machine instructions.
MSIL
We saw the do-while loop in depth, comparing it to the while-loop and the for-loop. The three loops did not have any performance difference, and the for-loop had shorter syntax. You will likely find that for loops are far more common than do-while loops. It is reasonable to avoid do-while loops—they are less familiar to developers and do not measurably change performance.
Loop Constructs