C# Language Looping Do - While Loop

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any C# Language Question?