C# Language Do - While Loop


Example

It is similar to a while loop, except that it tests the condition at the end of the loop body. The Do - While loop executes the loop once irrespective of whether the condition is true or not.

int[] numbers = new int[] { 6, 7, 8, 10 };
    
// Sum values from the array until we get a total that's greater than 10,
// or until we run out of values.
int sum = 0;
int i = 0;
do
{
    sum += numbers[i];
    i++;
} while (sum <= 10 && i < numbers.Length);
    
System.Console.WriteLine(sum); // 13